让我们考虑以下html代码:
<p>
Some text followed by <span>a span element</span>
and another text followed by <b>a bold text</b>.
</p>
Run Code Online (Sandbox Code Playgroud)
如何使用jQuery 获取span和b元素之前的文本?
我尝试了$("span").prev(),$("b").prev()但它确实有效,因为文本不是一个元素.我也尝试了,$("span").parent()但它匹配整段,而我只想要它的一部分.
你能告诉任何解决方案吗?先感谢您 :-)
Roa*_*rth 24
如何将它们收集到一个数组而不是两个调用$:
var texts = $('span, b').map(function(){
return this.previousSibling.nodeValue
});
texts[0]; // "Some text followed by "
texts[1]; // " and another text followed by "
Run Code Online (Sandbox Code Playgroud)
参考文献:
| 归档时间: |
|
| 查看次数: |
10199 次 |
| 最近记录: |