编写正确的 jquery 选择器以检索中断标记后但在 div 内的文本
我有一些格式如下的代码在我的页面中重复出现:
<div class="video-description-secondary" style="display:block;">
<h2 class="two">
Some title text
</h2>
<br/>
Some description text
</div>
这是我遇到的问题:我想使用 jquery 选择器检索描述文本(在 br 标记之后和结束 div 标记之前)。我不确定如何编写正确的 jquery 选择器来检索该文本,因为文本本身不在它自己的标签内。我需要对该文本应用一些代码,以便我可以正确实现阅读更多/更少链接。
任何帮助都会很棒。
I have some code in the format below that's repeated throughout my page:
<div class="video-description-secondary" style="display:block;">
<h2 class="two">
Some title text
</h2>
<br/>
Some description text
</div>
Here's the problem I'm running into: I want to retrieve the description text (after the br tag and before the closing div tag) using a jquery selector. I'm not sure how to write the proper jquery selector in order to retrieve that text since the text itself is not inside it's own tag. I need to apply some code against that text so that I can properly implement a read more/less link.
Any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样的事情对你有用吗?从技术上讲,它会抓取子标签之外的所有内容,而不是中断后的内容。
什么
替代解决方案
这使您可以更好地控制 被返回。检索所有文本节点,然后获取最后一个。
http://jsfiddle.net/aMkuY/3/
Would something like this work for you? Technically it grabs everything not inside child tags as opposed to whatever is after break.
http://jsfiddle.net/aMKuY/2/
Alternate Solution
This gives you slightly more control over what is returned. Retrieve all text nodes and then grab last one.
http://jsfiddle.net/aMKuY/3/
可以编写一个选择器,但我认为你应该重新评估你的设计。对您来说,只需将文本包装在某些标记中并选择它(这就是它的用途)会更容易。
所以这更好:
您可以像这样访问它:
更干净。
A selector for that can be written, but I think you should re-evaluate your design. It'd be much easier for you to just wrap the text in some markup and select that instead (that's what it's there for).
So this is better:
which you can access like:
Much cleaner.