你的追求有点令人困惑,但我会尽力让你完全分解许多方法中的一些来实现你的目标。
如果你想创建不是由特定的孩子,那么你可以简单的使用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(); });
您可能不会w蚂蚁阻止道具对所有track_options的孩子,因此你使用.filter()。这个方便的jQuery func可以让你停止精确地选择你选择的track_options的内部元素。见下面的例子:
// 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的链接在以前的代码中使用:
- .live()
- .on()
- .click()
- .find()
- .filter()
- .stopPropagation()
- jQuery Selectors
别的东西,可能的利益(我想示范例子,除了我必须去处理一个4岁的不想吃! grrr)是.andSelf()。它将允许拨打电话以获得track_options和其1或2个孩子,如下所示:$(".track_options).find("span:not(.track_options_addtoplaylist)").andSelf();