使用父子方法从链接中选择文本
我有一个代码如下;
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
会做的工作。但请记住,
$(.class1')
将匹配页面上具有类class1
的每个元素。因此您将获得所有内部链接的文本。您还可以这样做:
还有更多方法...我鼓励您查看 jQuery API 上的选择器和遍历部分文档。
祝你好运!
would do the job. Remember though, that
$(.class1')
will match every element on the page that has the classclass1
. So you'll get the text for all of the inner links.You could also do:
there are many more ways... I encourage you to check out the selectors and traversing section on the jQuery API docs.
Good luck!