所以如果我调用这个函数:
$("#item").text()
在此 HTML 代码上:
<div id="item">
<pre><span class="cm-tag"><html></span></pre><pre><span class="cm-tab"> </span>asdf</pre><pre><span class="cm-tag"></html></span></pre>
</div>
它返回:
'<html> asdf</html>'
我希望它返回:
'<html>
asdf
</html>'
基本上我需要在每个 <pre>
之后换行标记...我该怎么做?
最佳答案
一个可能的解决方案,获取每个pre的文本并用新行连接它们:
var text = $("#item pre").map(function(){
return $(this).text();
}).get().join('\n');
这是 jsfiddle:http://jsfiddle.net/uGGFe/
关于javascript - 返回标签的文本值并为每个标签附加一个换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11200692/