我有以下html:
<em id="work-resize">re-size: from <span id="work-resize-from">3</span> to <span id="work-resize-to">5</span></em>
使用jQuery我想得到以下字符串:
're-size: from 3 to 5'
我认为使用.text()
会完全相同,但是:
$('#work-resize').text();
返回:
're-size: from <span id="work-resize-from">3</span> to <span id="work-resize-to">5</span>'
我达到了顶峰,我找不到原因。我可以使用这里建议的函数remove tags which javascript returns with the text,但我真的认为.text()
的要点是删除标签,所以使用我自己的函数似乎是多余的。如果我做错了什么,有没有人有更好的解决方案或建议?
答案 0 :(得分:3)
.html()
:
$('<em id="work-resize">re-size: from <span id="work-resize-from">3</span> to <span id="work-resize-to">5</span></em>').text()
"re-size: from 3 to 5"
$('<em id="work-resize">re-size: from <span id="work-resize-from">3</span> to <span id="work-resize-to">5</span></em>').html()
"re-size: from <span id="work-resize-from">3</span> to <span id="work-resize-to">5</span>"
答案 1 :(得分:1)
见这里。 http://jsfiddle.net/Vu5ck/1/
下次使用类似的http://jsfiddle.net报告javascript问题。