JQuery 单击除某些 div 之外的任何位置以及动态添加 html 的问题
我希望该网页在您单击其中一个元素时突出显示某些元素,但如果您单击页面上的其他任何位置,则所有这些元素不应再突出显示。
我通过以下方式完成了此任务,除了一件事(如下所述)之外,它工作得很好:
$(document).click(function() {
// Do stuff when clicking anywhere but on elements of class suggestion_box
$(".suggestion_box").css('background-color', '#FFFFFF');
});
$(".suggestion_box").click(function() {
// means you clicked on an object belonging to class suggestion_box
return false;
});
// the code for handling onclicks for each element
function clickSuggestion() {
// remove all highlighting
$(".suggestion_box").css('background-color', '#FFFFFF');
// then highlight only a specific item
$("div[name=" + arguments[0] + "]").css('background-color', '#CCFFCC');
}
这种强制突出显示元素的方法工作得很好,直到我向页面添加更多 html 而无需加载新页面。这是通过 .append() 和 .prepend() 完成的。
我从调试中怀疑页面没有“意识到”动态添加到页面的新元素。尽管新的、动态添加的元素具有适当的类名/ID/名称/onclicks 等,但它们永远不会像其他元素一样突出显示(它们始终可以正常工作)。
我想知道我的方法不适用于动态添加的内容的可能原因是否是页面无法识别页面加载期间不存在的元素。如果这是可能的,那么有没有办法在没有页面加载的情况下协调这个问题?
如果这条推理是错误的,那么我上面的代码可能不足以显示我的网页出了什么问题。但我真的只是对这种想法是否可行感兴趣。
I want this webpage to highlight certain elements when you click on one of them, but if you click anywhere else on the page, then all of these elements should no longer be highlighted.
I accomplished this task by the following, and it works just fine except for one thing (described below):
$(document).click(function() {
// Do stuff when clicking anywhere but on elements of class suggestion_box
$(".suggestion_box").css('background-color', '#FFFFFF');
});
$(".suggestion_box").click(function() {
// means you clicked on an object belonging to class suggestion_box
return false;
});
// the code for handling onclicks for each element
function clickSuggestion() {
// remove all highlighting
$(".suggestion_box").css('background-color', '#FFFFFF');
// then highlight only a specific item
$("div[name=" + arguments[0] + "]").css('background-color', '#CCFFCC');
}
This way of enforcing the highlighting of elements works fine until I add more html to the page without having a new page load. This is done by .append() and .prepend()
What I suspected from debugging was that the page is not "aware" of the new elements that were added to the page dynamically. Despite the new, dynamically added elements having the appropriate class names/IDs/names/onclicks ect, they never get highlighted like the rest of the elements (which continue to work fine the entire time).
I was wondering if a possible reason for why my approach does not work for the dynamically added content is that the page is not able to recognize the elements that were not present during the pageload. And if this is a possibility, then is there a way to reconcile this without a pageload?
If this line of reasoning is wrong, then the code I have above is probably not enough to show what's wrong with my webpage. But I'm really just interested in whether or not this line of thought is a possibility.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
.live
来“将处理程序附加到事件与当前选择器匹配的所有元素,现在和将来”。示例:另请参阅
.delegate
,这是类似的。Use
.live
to "Attach a handler to the event for all elements which match the current selector, now and in the future". Example:Also see
.delegate
, which is similar.来自 jQuery 文档 =)
(只是为了更好地解释为什么 @karim79 还建议使用委托方法 ;P )
from the jQuery documentation =)
(only to explain better why @karim79 also suggested the delegate method ;P )