我在div中选择一些段落:
$('#myDiv p.p1, #myDiv p.p2').text();
Run Code Online (Sandbox Code Playgroud)
我的问题是我想在每个选择之间添加空格,以便输出为:"paragraph1(space)paragraph2"而不是"paragraph1paragraph2".
有任何想法吗?
您可以使用.map()with .get()来创建单独段落的数组,然后使用.join(" ")它们将它们连接在一起,其间有空格.
试试这个:
var result = $('#myDiv p.p1, #myDiv p.p2').map(function() {
return $.text([this]);
// return this.innerHTML; // Alternate means of getting text
// return this.firstChild.nodeValue; // Another alternate
}).get().join(" ");
Run Code Online (Sandbox Code Playgroud)
该result变量应该有你的段落分隔它们的空间.
编辑:根据@JP的评论,更新文本检索更有效.
| 归档时间: |
|
| 查看次数: |
1254 次 |
| 最近记录: |