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

前端 未结 4 914
庸人自扰
庸人自扰 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: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());
    

提交回复
热议问题