[英]jQuery - removing a div element
I'm doing a project, where I got stuck with some user interfacing tweaks, where I need to remove some HTML div elements. 我正在做一个项目,在其中进行了一些用户界面调整,需要删除一些HTML div元素。
Please check this code http://jsfiddle.net/YVZH5/ If you click the text you will see the blue tag. 请检查此代码http://jsfiddle.net/YVZH5/如果单击文本,则会看到蓝色标记。 I need to remove that tag when the user clicks the X. But it is not working.
当用户单击X时,我需要删除该标签。但是它不起作用。
And It is working here http://jsfiddle.net/Jst4v/ So, where is the problem occurs? 它在这里工作http://jsfiddle.net/Jst4v/那么,问题出在哪里?
Thanks! 谢谢!
you need to chage 你需要挑战
$("#service-sets").click(function(){
$("#facebook-tag").remove();
});
to 至
$("#service-sets").on('click', '#facebook-del', function(){
$("#facebook-tag").remove();
});
Because, #facebook-del
(cross icon) will remove the tags and they come to DOM after DOM ready, so they need delegate ie live events. 因为
#facebook-del
(十字图标)将删除标记,并且在DOM准备就绪后它们才会到达DOM,因此它们需要委托(即实时事件)。
The reason it works in your first fiddle and not in your second is because the element you're trying to attach a click function to (facebook-del) does not exist in the DOM at the time of assignment. 它在您的第一个小提琴中起作用而不是在您的第二个中起作用的原因是,在分配时,您试图将click函数附加到(facebook-del)的元素在DOM中不存在。
In the second example, the element exists on page load and thus gets assigned the click function. 在第二个示例中,元素在页面加载时存在,因此被分配了click函数。
Try looking at .on() as a way to assign your click events to dynamically added elements like this. 尝试查看.on(),将点击事件分配给这样动态添加的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.