gpt4 book ai didi

jQuery 选择器 - child 的 child

转载 作者:行者123 更新时间:2023-12-01 06:30:36 26 4
gpt4 key购买 nike

我有以下 jquery 片段(与 each 一起使用来迭代结果表):

$('.foo').children('.bar').children('.first_baz').text();
$('.foo').children('.bar').children('.second_baz').text();

我用它来访问$firstvalue$secondvalue:

<tr class='foo'>
<td class='bar'><span class='first_baz'>".$firstvalue."</span><span class='second_baz'>".$secondvalue."</span></td>
</tr>

这可行,但是有效吗?我怀疑有更好的方法来访问这些值...它是什么?

最佳答案

如果 first_baz 和 secondary_baz 元素仅出现在表格中,并且页面中没有其他位置,那么您可以使用以下代码片段跳过子函数:

$('.first_baz').text(); 
$('.second_baz').text();

如果上述情况不成立,那么您可以将语句缩短为

var parent = $('.foo'); //Get the parent context so that its not queried twice
$('.first_baz', parent).text();
$('.second_baz', parent).text();

关于jQuery 选择器 - child 的 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5572881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com