insertAfter

编程入门 行业动态 更新时间:2024-10-21 05:52:49
本文介绍了insertAfter - find - on() - 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想,我已经设置好了,但它仍然不工作 - 我正在用 X 删除标志。如果我点击新添加的 X 符号,应该删除新添加的输入。我做的是: $ b $ pre $ function addmore(){ $('< input type =filename ='bild []/>< a class =del> X< / a>< br />')。 ('click',function(){ alert('test'); }); }

我的html是:

< a href =javascript:addmore()>添加更多< / a> < p id =more>< / p>

它插入的很好,但我无法删除新添加的元素(警报不会触发)。为什么是这样?

非常感谢解决方案

.find()函数在当前的匹配元素集合内查找元素,但不包含那些匹配的元素。您的< a class =del> 元素是匹配元素集合的一部分,因此不会被拾取。

使用 .filter()

i think, i have all set, but it is still not working - i am appending new html element input with X delete sign. if i click on that newly added X sign, the newly added input should be deleted. what i did is:

function addmore(){ $('<input type="file" name="bild[]"/> <a class="del">X</a> <br/>').insertAfter('#more').find('.del').on('click', function(){ alert('test'); }); }

my html is:

<a href="javascript:addmore()"> add more </a> <p id="more"></p>

it is inserting well, but i cannot delete the newly added element (alert is not firing). why is this?

thanks a lot

解决方案

The .find() function looks for elements that are inside the current set of matched elements, but doesn't include those matched elements. Your <a class="del"> element is part of the set of matched elements so won't be picked up.

Use .filter() instead:

....filter('.del').on('click', function() {...});

更多推荐

insertAfter

本文发布于:2023-10-26 18:52:54,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:insertAfter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!