Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 将事件添加到在iframe中动态添加的元素_Jquery_Html_Iframe_Javascript Events - Fatal编程技术网

Jquery 将事件添加到在iframe中动态添加的元素

Jquery 将事件添加到在iframe中动态添加的元素,jquery,html,iframe,javascript-events,Jquery,Html,Iframe,Javascript Events,我有这样的HTML代码 <iframe id="myframe"> <ul> <li> <a href="" class="add-section">add section</a> <div>...</div> </li> <ul> </iframe> ... 当点击

我有这样的HTML代码

<iframe id="myframe">
    <ul>
        <li>
            <a href="" class="add-section">add section</a>
            <div>...</div>
        </li>
     <ul>
</iframe>

  • ...
当点击“添加节”链接时,包含此(添加节)链接的parrent
  • 将被复制并插入,代码如下所示

    <iframe id="myframe">
        <ul>
            <li>
                <a href="" class="add-section">add section</a>
                <div>...</div>
            </li>
            <li>
                <a href="" class="add-section">add section</a>
                <div>...</div>
            </li>
         <ul>
    </iframe>
    
    
    
    • ...
    • ...
    我使用这个jquery代码来实现这一点,它工作得很好

    $($("#myframe").contents().find('.add-section')).on("click",function(e){
        e.preventDefault();
    
        $(this).parent().after("<li>"+$(this).parent().html()+"</li>");
    
    });
    
    $($(“#myframe”).contents().find('.add section')).on(“单击”,函数(e){
    e、 预防默认值();
    $(this.parent())之后(“
  • ”+$(this.parent().html()+”
  • ”); });
    我的问题是,当我单击新建(添加部分)链接时,代码将不会执行

    我的问题是如何将单击事件添加到iframe中最新添加的元素中


    提前感谢。

    尝试将事件绑定到body元素,然后使用事件委派,如

    $("#myframe").contents().find('body').on("click", '.add-section', function (e) {
        e.preventDefault();
        $(this).parent().after("<li>" + $(this).parent().html() + "</li>");
    });
    
    $(“#myframe”).contents().find('body')。在(“单击“,”。添加节”,函数(e){
    e、 预防默认值();
    $(this.parent())之后(“
  • ”+$(this.parent().html()+”
  • ”); });
    尝试将事件绑定到body元素,然后使用事件委派,如

    $("#myframe").contents().find('body').on("click", '.add-section', function (e) {
        e.preventDefault();
        $(this).parent().after("<li>" + $(this).parent().html() + "</li>");
    });
    
    $(“#myframe”).contents().find('body')。在(“单击“,”。添加节”,函数(e){
    e、 预防默认值();
    $(this.parent())之后(“
  • ”+$(this.parent().html()+”
  • ”); });
    尝试将事件绑定到body元素,然后使用事件委派,如

    $("#myframe").contents().find('body').on("click", '.add-section', function (e) {
        e.preventDefault();
        $(this).parent().after("<li>" + $(this).parent().html() + "</li>");
    });
    
    $(“#myframe”).contents().find('body')。在(“单击“,”。添加节”,函数(e){
    e、 预防默认值();
    $(this.parent())之后(“
  • ”+$(this.parent().html()+”
  • ”); });
    尝试将事件绑定到body元素,然后使用事件委派,如

    $("#myframe").contents().find('body').on("click", '.add-section', function (e) {
        e.preventDefault();
        $(this).parent().after("<li>" + $(this).parent().html() + "</li>");
    });
    
    $(“#myframe”).contents().find('body')。在(“单击“,”。添加节”,函数(e){
    e、 预防默认值();
    $(this.parent())之后(“
  • ”+$(this.parent().html()+”
  • ”); });