你的任务有点令人困惑,但我会尝试对实现目标的众多方法中的几个进行完整的分解。
如果您尝试为未由特定子级触发的父级 Div 创建单击,则可以简单地使用event.stopPropagation(),如下所示:
// Noticed I did not use an ID call here for your parent div, the reason is simple,
// You stated you use it like a "template" and clone it, or at least parts from it, thus it might
// (depending on how you use it) have multiple positions in your document, thus, one set ID just
// will not do. So I pretended as if you had already added a similar named class to the parent
// div, thus calling forth this click function on ALL div's containing said class
$(".build_blankrow")
// This call to .live will ensure you can call the click function dynamically
// on "future" created divs containing the same class name
.live("click", function(e) { /* do work */ })
// This is called "chaining" in jquery
// Our .live click func returns the originally called '$(".build_blankrow")'
// ALSO: in NEWER jQuery, .live is replaced with .on
// Thus we dont need to make a new call just to get to its childrean
// .find will allow us to search the children for exactly what we need
// in this case we're grabbing the span with the class 'track_options'
// and setting its click func (this will effect its children) to stop propagation to parents
.find(".track_options")
.live("click", function(e) { e.stopPropagation(); });
您可能不希望 track_options 的所有孩子都使用 stop 道具,因此您使用.filter()。这个方便的 jQuery 函数将允许您在您选择的 track_options 的内部元素上停止 prop。请参见下面的示例:
// You will notice not much change at start
$(".build_blankrow")
.live("click", function(e) { /* do work */ })
.find(".track_options span")
// Here comes the change, gota love .filter
// Here I will get only the children elements for download and play now
.filter(".track_options_download, .track_options_playnow")
// KEEP IN MIND, if your using NEWEST jQuery, then replace .live with .on
// like so: .on("click", funct.....
.on("click", function(e) { e.stopPropagation(); console.log("WHAT"); });
您可以利用 jQuery 中的 CSS 选择器来想出巧妙的方法来根据需要访问每个元素。就像是:
$(".build_blankrow")
.on("click", function(e) { /* do work */ })
// Here I use simple CSS to filter out the autoplaylist child
.find(".track_options span:not(.track_options_addtoplaylist)")
.on("click", function(e) { e.stopPropagation(); });
之前代码中使用的重要 jQuery 链接:
- 。居住()
- 。在()
- 。点击()
- 。寻找()
- 。筛选()
- .stopPropagation()
- jQuery 选择器
其他可能感兴趣的东西(我会举个例子,除了我必须和一个不想吃饭的 4 岁孩子打交道!grrr)是.andSelf()。它将允许调用获取 track_options 及其 1 或 2 个子项,如下所示:$(".track_options).find("span:not(.track_options_addtoplaylist)").andSelf();