使用父子方法从链接中选择文本

发布于 2024-11-30 18:09:17 字数 335 浏览 4 评论 0原文

我有一个代码如下;

<td class="class1" colspan="5">
<a class="class2" href="LINKLINK">TEXTDATA</a>
</td>

我可以通过 ('.class2').text() 来选择链接文本。但我想使用儿童方法来获取文本。如果我没有链接的类(class2)并且只有 class1,我怎样才能获取相同的内容。我认为解决方案类似于 ('.class1').children('a').text()

我该怎么做?

I have a code as below;

<td class="class1" colspan="5">
<a class="class2" href="LINKLINK">TEXTDATA</a>
</td>

I can select the link text by taking ('.class2').text(). But I want to grab the text using children method. How can I grab the same if I have no class for the link (class2) and have only class1. I think the solution will be something like ('.class1').children('a').text().

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┈┾☆殇 2024-12-07 18:09:17
$('.class1').children('a').text();

会做的工作。但请记住,$(.class1') 将匹配页面上具有类 class1 的每个元素。因此您将获得所有内部链接的文本。

您还可以这样做:

$('.class1:first').children('a').text();
$('.class1').first().children('a').text();
$('.class1').find('a').text(); //checks all descendants
var context = $('.class1')[0];
$('a', context).text();

还有更多方法...我鼓励您查看 jQuery API 上的选择器和遍历部分文档

祝你好运!

$('.class1').children('a').text();

would do the job. Remember though, that $(.class1') will match every element on the page that has the class class1. So you'll get the text for all of the inner links.

You could also do:

$('.class1:first').children('a').text();
$('.class1').first().children('a').text();
$('.class1').find('a').text(); //checks all descendants
var context = $('.class1')[0];
$('a', context).text();

there are many more ways... I encourage you to check out the selectors and traversing section on the jQuery API docs.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文