2012-01-16 103 views
1

我试图弄清楚这样的事情是否可能。我们给出了HTML结构jQuery CSS选择器与“this”相结合

<a href='#' class='anyLink'> 
    <!-- here goes some content--> 
    <div class='childElement'><!-- here goes some content--></div> 
</a> 

我不能使用ID的,因为有很多链接,它没有定义多少更多的来。所以我的问题是,你们知道的方式,我可以做这样的事情:

$('a').on("click",function(e){ 
    $(this +"div").val(); // for example. 
}); 

我要选择已单击锚的儿童元素或希望得到孩子们元素的值。我也没有任何孩子元素的ID,我试图通过CSS选择器选择td:nth-child(4)。 有人可以告诉我这是可能的吗?

回答

7

尝试

$('a').on("click",function(e){ 
    $("div",this).text(); // for example. 
}); 
+0

这是否也与CSS选择器的工作? – 2012-01-16 15:52:05

+0

我喜欢并使用第二个建议。 – jeanreis 2012-01-16 15:53:21

+0

@EvilP yup ..... – Rafay 2012-01-16 15:55:36

0
$('a').on("click",function(e){ 
    $(this).children("div").eq(0).html(); 
}); 
0

你正在寻找一个叫做.children()功能。

但你也可以尝试这样的事:

$('a').on('click', function(e) { 
    $('div', this).val(); // Each div child of this element 
    $(this).children('div'); // Each div child of this element 
}); 
+0

抱歉,但我alery意识到.children。我必须通过CSS选择器访问 – 2012-01-16 15:53:26

+0

您可以使用选择器作为儿童的第一个参数。 [现场示例](http://jsfiddle.net/NvwMg/) – 2012-01-16 15:57:10