Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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_Event Delegation - Fatal编程技术网

Javascript 删除动态填充的<;李>;仅当数组包含奇数项时有效

Javascript 删除动态填充的<;李>;仅当数组包含奇数项时有效,javascript,jquery,event-delegation,Javascript,Jquery,Event Delegation,项目实例: enterItem=[] itemNumber=0 //将项目标记为已选中并添加删除功能 函数检查(){ $('#itemslist')。在('click','li',function(){ $(this.toggleClass('checked'); $(this.append(“”); $(this.append('X'); $(this).not('.checked').find('hr').remove(); $(this).not('.checked').find('but

项目实例:

enterItem=[]
itemNumber=0
//将项目标记为已选中并添加删除功能
函数检查(){
$('#itemslist')。在('click','li',function(){
$(this.toggleClass('checked');
$(this.append(“
”); $(this.append('X'); $(this).not('.checked').find('hr').remove(); $(this).not('.checked').find('button').remove(); //启用垃圾箱或删除单个列表项的功能 $('.trash')。在('click',function()上{ $(this).最近('li').remove(); }); }); }
这仅在itemNumber为奇数或enterItem包含奇数个项目时正确执行


查看小提琴以检查我是如何将项目推入数组的,以及我的增量技术,这是因为您要一次又一次地绑定同一事件。每次添加项目时,都会为单击事件绑定另一个处理程序

添加偶数个项目后,会添加偶数个事件处理程序。当您单击一个项目时,第二个处理程序将撤消第一个处理程序所做的操作

例如,当您有十个项目时,事件处理程序将被调用十次。由于最终结果与调用事件处理程序的次数为零相同,因此似乎什么都没有发生

只需绑定事件处理程序一次,而不是每次添加项目时。

只需执行以下操作:

 //   function check() {
        $('#itemsListed').on('click', 'li', function() {    
            $(this).toggleClass('checked');
            $(this).append('<hr />');
            $(this).append('<button class="trash">X</button>');
            $(this).not('.checked').find('hr').remove();
            $(this).not('.checked').find('button').remove();
            // enable trash or delete functionality individual list items
            $('.trash').on('click', function() {
                $(this).closest('li').remove();
            });
        });
   // }
//函数检查(){
$('#itemslist')。在('click','li',function(){
$(this.toggleClass('checked');
$(this.append(“
”); $(this.append('X'); $(this).not('.checked').find('hr').remove(); $(this).not('.checked').find('button').remove(); //启用垃圾箱或删除单个列表项的功能 $('.trash')。在('click',function()上{ $(this).最近('li').remove(); }); }); // }

将绑定移到check()函数之外

该代码根本不使用
enterItem
itemNumber
,因此它不会受到这些变量所包含内容的合理影响。“正确执行”是什么意思,即发生了什么,这与预期有什么不同?请查看小提琴。。。我无法在帖子中添加所有内容。它应该添加一个“checked”的css类,并启用一个按钮,如果选中该类,可以将其删除。但只有当enterItem中的项目数为奇数时,它才起作用
 //   function check() {
        $('#itemsListed').on('click', 'li', function() {    
            $(this).toggleClass('checked');
            $(this).append('<hr />');
            $(this).append('<button class="trash">X</button>');
            $(this).not('.checked').find('hr').remove();
            $(this).not('.checked').find('button').remove();
            // enable trash or delete functionality individual list items
            $('.trash').on('click', function() {
                $(this).closest('li').remove();
            });
        });
   // }