Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.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
Javascript 向动态创建的元素添加自定义触发器_Javascript_Jquery - Fatal编程技术网

Javascript 向动态创建的元素添加自定义触发器

Javascript 向动态创建的元素添加自定义触发器,javascript,jquery,Javascript,Jquery,我需要为动态创建的元素创建一个触发器,然后使用事件定义来获取事件并删除动态元素 <div class="row"> <div class="col-md-8 clickable"></div> <div class="col-md-4 display-clicks"> <ul class="clicks"> </ul> </div> </div>

我需要为动态创建的元素创建一个触发器,然后使用事件定义来获取事件并删除动态元素

<div class="row">
    <div class="col-md-8 clickable"></div>
    <div class="col-md-4 display-clicks">
        <ul class="clicks">
        </ul>
    </div>
</div>
myEvent看起来像这样

   $('.clickable').on('myEvent', 'span', function(event){
       $(this).remove();
    })
有什么想法吗?

$(this.parent().remove();
也将删除
元素,因为您正在删除它的父元素。因此,
id
无法定义,您无法选择量程。把那条线放在街区的尽头。此外,您的id不能以数字开头-

请尝试以下操作:

   $('.clickable').on('myEvent', 'span', function(event){
       $(this).remove();
    })
你的函数是这样的:

  $('.display-clicks').on('click','ul',function(){
        $(this).parent().remove();
        // and this gets me the id of the dynamic span to be removed
        // from the clickable div
        var id = $(this).data('span-id');

        // this works now
        $("#" + id+'_span_just_remove').remove();

        // this works too
        $("#" + id +'_span_trigger').trigger('myEvent');
  })

Plunker:

$('clickable')=>$('clickable'))
是的,很抱歉,这只是一个输入错误。clickable在真实的代码中,动态附加的
html
是什么样子的?它只是一个带有id的span,如
x
我可以看到它看起来是这样的,在删除父变量之前我实际上将id强制转换为一个变量,我可以控制台。尝试删除span后记录它,我将编辑问题代码。也不要在
id
值的开头使用数字,您可以在第一个字符后使用它们-这应该是一个字母非常奇怪我的代码似乎可以正常运行,但在我的开发机器上无法运行抱歉
  $('.display-clicks').on('click','ul',function(){
        $(this).parent().remove();
        // and this gets me the id of the dynamic span to be removed
        // from the clickable div
        var id = $(this).data('span-id');

        // this works now
        $("#" + id+'_span_just_remove').remove();

        // this works too
        $("#" + id +'_span_trigger').trigger('myEvent');
  })