本文介绍了使用jQuery从字符串中删除跨度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在按Enter键时获取p标签的文本值
I am wanting to take the text value of a p tag when I press enter
$.keynav.enterDown = function () {
var accom = $('#suggestions p.keynavon').html();
alert(accom);
}
这很好用,但是结果包含<span>
和</span>
标记,反正我可以从字符串中剥离这些标记吗?
This is working great but the results contain <span>
and </span>
tags, is there anyway I can strip those tags from the string?
推荐答案
如果您只想要听起来像是您想要的文本,请使用 .text()
而不是 .html()
这个:
If you just want the text, which sounds like what you're after, use .text()
instead of .html()
, like this:
$.keynav.enterDown = function () {
var accom = $('#suggestions p.keynavon').text();
alert(accom);
}
如果您确实需要专门剥离<span>
标记,请克隆内容(以免影响原始内容),然后通过 .replaceWith()
,如下所示:
If you actually need to strip the <span>
tags specifically, clone the content (as to not affect the original) and replace them with their inner content via .replaceWith()
, like this:
$.keynav.enterDown = function () {
var accom = $('#suggestions p.keynavon').clone()
.find('span').replaceWith(function() { return this.innerHTML; })
.end().html();
alert(accom);
}
这篇关于使用jQuery从字符串中删除跨度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!