[英]Why doesn't this jQuery code work?
I have this jQuery code: 我有这个jQuery代码:
$(".topic_form").hide();
$("#edit_topics_link").click(function(e) {
e.preventDefault();
$(".topic_form").show();
$(this).hide();
$("<a href='#' id='done_link'>Done</a>").insertBefore(".topic_form");
});
$("#done_link").click(function(e) {
e.preventDefault();
$(this).remove();
$(".topic_form").hide();
$("#edit_topics_link").show();
});
The first half of the code does this: it hides a form when the page loads. 代码的前半部分是这样做的:当页面加载时,它隐藏了一个表单。 Then when you click a link, it shows the form, hides the clicked link, and adds a new link.
然后,当您单击链接时,它会显示表单,隐藏单击的链接并添加新的链接。 This works 100% fine.
这可以100%罚款。
The 2nd half of the code doesn't work. 代码的第二部分不起作用。 When you click the newly added link, it should remove it, show the old link, and re-hide the form.
单击新添加的链接时,它应将其删除,显示旧链接,然后重新隐藏表单。 Nothing happens when I click the newly added link.
单击新添加的链接没有任何反应。 Why is this?
为什么是这样? How can I fix it?
我该如何解决?
Because the element to which you're attaching the click-handler to doesn't exist at the time of the document's loading, there are no events attached. 因为在文档加载时不将单击处理程序附加到的元素,所以没有附加事件。 You should be able to use
live()
to fix this: 您应该可以使用
live()
解决此问题:
$("#done_link").live('click', function(e) {
e.preventDefault();
$(this).remove();
$(".topic_form").hide();
$("#edit_topics_link").show();
});
The .live() method is able to affect elements that have not yet been added to the DOM through the use of event delegation: a handler bound to an ancestor element is responsible for events that are triggered on its descendants.
.live()方法能够通过使用事件委托来影响尚未添加到DOM的元素:绑定到祖先元素的处理程序负责在其后代上触发的事件。
You could also use delegate()
on the ancestor element of the #done_link
element: 您还可以在
#done_link
元素的祖先元素上使用delegate()
:
$("#parentElementSelector').delegate('#done_link', 'click', function(e) {
e.preventDefault();
$(this).remove();
$(".topic_form").hide();
$("#edit_topics_link").show();
});
References: 参考文献:
live()
. live()
。 delegate()
. delegate()
。 The reason it doesn't work is that when you call the second part of the script the link doesn't exist yet. 它不起作用的原因是,当您调用脚本的第二部分时,该链接尚不存在。 There are 2 ways to solve it.
有两种解决方法。 Either move the second part into the first part.
将第二部分移到第一部分。 That way the attachment of the event handler happens when the link exists:
这样,当链接存在时,将发生事件处理程序的附件:
$(".topic_form").hide();
$("#edit_topics_link").click(function(e) {
e.preventDefault();
$(".topic_form").show();
$(this).hide();
$("<a href='#' id='done_link'>Done</a>").insertBefore(".topic_form")
.click(function(e) {
e.preventDefault();
$(this).remove();
$(".topic_form").hide();
$("#edit_topics_link").show();
});
});
Or use the live
method to assign the handler. 或使用
live
方法分配处理程序。 The live
method will watch the dom and whenever something with the selector you specified (in this case .topic_form
) appears it will attach the event to it: live
方法将监视dom,并且每当使用您指定的选择器(在本例中为.topic_form
)出现时,它将.topic_form
附加事件:
$(".topic_form").hide();
$("#edit_topics_link").click(function(e) {
e.preventDefault();
$(".topic_form").show();
$(this).hide();
$("<a href='#' id='done_link'>Done</a>").insertBefore(".topic_form");
});
$("#done_link").live('click', function(e) {
e.preventDefault();
$(this).remove();
$(".topic_form").hide();
$("#edit_topics_link").show();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.