从 jquery 中的段落中获取文本
这在 jquery 中可能是一件非常简单的事情,但我无法弄清楚。我的 html 文档具有以下结构
<div class="body">
<a href="/question?id=70"><p>This is the text I want to extract</p></a>
</div>
我尝试过,
$("body").find("a p").text()
但这似乎对我不起作用。我能够获取段落对象,但不能获取文本。我用console.log测试过,没有用。
This might be a very simple thing in jquery but I am not able to figure it out. My html document has the following structure
<div class="body">
<a href="/question?id=70"><p>This is the text I want to extract</p></a>
</div>
I tried this
$("body").find("a p").text()
but this does not seem to be working for me. I am able to get the paragraph object but not the text. I tested it with console.log with no use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你所拥有的应该可以工作(你可以在这里测试它),确保你在运行它时不过 DOM 已经准备好了,就像这样:
如果它运行得更早,元素可能还没有准备好,并且您的选择器将找不到任何匹配项。
如果您想选择class
body
,请确保使用".body"
而不是"body"
(这将选择元素)。这是使用
.class
选择器的版本:What you have should be working (you can test it here), make sure you're running it when the DOM is ready though, like this:
If it runs earlier, the elements may not be ready, and your selector won't find any matches.
If you want to select the class
body
make sure to use".body"
instead of"body"
(which would select the<body>
element). Here's a version using the.class
selector:.html() 函数检索节点内部 html。
应该能解决问题
the .html() function retrieves a nodes inner html.
should do the trick
不确定这是否会导致问题,但您的标记无效。来自“HTML 文档的全局结构”由 W3C
a
元素应该包含在p
等块元素中,而不是相反。Not sure if this would cause a problem, but you have invalid markup. From "The global structure of an HTML document" by the W3C
a
elements are supposed to be contained by block elements likep
, not the other way around.这是 html 或 php 文件中的段落元素,它是 id 分配 tt
in in jquery file to get the text of your paragraph with tt id can be
对于 jquery-3.1.1.js 工作正常
here is your paragraph element in html or php file which is id assign tt
in in jquery file to get the text of your paragraph with tt id can be
working fine for jquery-3.1.1.js