开发者

strip span tags from string with jquery

开发者 https://www.devze.com 2023-01-17 10:13 出处:网络
I am wanting to take the text value of a p tag when I press enter $.keynav.enterDown = function () { var ac开发者_开发技巧com = $(\'#suggestions p.keynavon\').html();

I am wanting to take the text value of a p tag when I press enter

$.keynav.enterDown = function () {
  var ac开发者_开发技巧com = $('#suggestions p.keynavon').html();
  alert(accom);

}

This is working great but the results contain <span> and </span> tags, is there anyway I can strip those tags from the string?


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);
}

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消