how do I strip white space when grabbing text with jQuery?

前端 未结 4 899
庸人自扰
庸人自扰 2020-11-27 10:16

I\'m wanting to use jQuery to wrap a mailto: anchor around an email address, but it\'s also grabbing the whitepace that the CMS is generating.

Here\'s the HTML I hav

相关标签:
4条回答
  • 2020-11-27 11:02

    Actually, jQuery has a built in trim function:

     var emailAdd = jQuery.trim($(this).text());
    

    See here for details.

    0 讨论(0)
  • 2020-11-27 11:06

    Javascript has built in trim:

    str.trim()
    

    It doesn't work in IE8. If you have to support older browsers, use Tuxmentat's or Paul's answer.

    0 讨论(0)
  • 2020-11-27 11:08

    Use the replace function in js:

    var emailAdd = $(this).text().replace(/ /g,'');
    

    That will remove all the spaces

    If you want to remove the leading and trailing whitespace only, use the jQuery $.trim method :

    var emailAdd = $.trim($(this).text());
    
    0 讨论(0)
  • 2020-11-27 11:11

    str=str.replace(/^\s+|\s+$/g,'');

    0 讨论(0)
提交回复
热议问题