jQuery,选择嵌套元素

时间:2010-09-16 15:30:06

标签: javascript jquery jquery-selectors

我选择

$("a.tp[programm='" + programm + "']");

然后我想选择它的嵌套元素 span.thump 并设置它的文本。我怎么意识到这一点?

<h4><a programm="74" class="tp" href="#"><img src="/images/tuo.png"></a><a href="">
<img width="180" height="40" src="/images/kauf_default.png"><br>test 
<span class="thump">1</span><img src="/images/ilike.png"></a></h4>

2 个答案:

答案 0 :(得分:3)

你的意思是

<a class="tp" programm="foo">blah <span class="thump">setTextOfThis</span> blah</a>

?如果是这样,请尝试

$("a.tp[programm='" + programm + "'] span.thump").text(newText);

编辑:关于更新,请尝试

$("a.tp[programm='" + programm + "'] + a span.thump").text(newText);

(如果包含... ~ a span.thump的{​​{1}}不在<a>旁边,则您可能需要<span>

答案 1 :(得分:0)

嗯,您要查找的<span>不是您选择的<a>代码的子代,因此您需要遍历.parent()节点和{{3}正确的孩子。

$("a.tp[programm='" + programm + "']").parent().find('.thump');
相关问题