2012-07-30 54 views
1

我可以解释不好,但是我不知道如何来形容,反正..一个变种修改jQuery选择

我有这样的变种:

var d_atributo=$("div.atributo"); 

有无论如何做选择提到这种方式的元素孩子?

d_atributo.$(" span.example).click(function(){...}); 

我的意思是我想要使用这个var并且在飞行中扩展他的“定义”。

+0

你可以使用['.find()'](http://api.jquery.com/find/) – m90 2012-07-30 08:07:09

回答

5

号你不能做到这一点,通过默认jQuery选择自己的方式。

你可以这样做:

d_atributo.find("span.example").click(function(){ 
    // your code 
}); 

OR

d_atributo.children("span.example").click(function(){ 
    // your code 
}); 

OR

$("span.example", d_atributo).click(function() { 
// your code 
}); 
+0

Upvoted给了一些不同的选择。请注意,以OP的是,第二个选项(使用儿童)的行为不同,只发现元素是d_attributo – 2012-07-30 08:11:55

+0

的直接子@CharlieRudenstål感谢队友 – thecodeparadox 2012-07-30 08:13:46

+0

三江源这么多thecodeparadox! – jsertx 2012-07-30 08:17:33

0

改用

d_atributo.find("span.example").click(function(){...}); 

或使用children()代替find()如果你正在寻找即时儿童

0

看来你要寻找的children()方法:

d_atributo.children("span.example").click(function() { 
    // ... 
}); 
1

你可以别名find

jQuery.fn.$ = jQuery.fn.find; 

然后它会工作:

var d_atributo = $("div.atributo"); 
d_atributo.$("span.example").click(function() { 
    alert('hello'); 
}); 

演示:http://jsfiddle.net/9rFBS/1/

+0

我喜欢这个答案。 (不过,我会强烈反对这样做) – 2012-07-30 08:17:51

+1

嘿啊,它只是为了让这个有趣的:P他问“这样”毕竟 – Esailija 2012-07-30 08:20:41