最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery replace is not working - Stack Overflow

programmeradmin0浏览0评论

DEMO

Hi Am trying to remove the arrows from JS. but seems like replace is not working :-(

<a class="pr-page-prev">« Previous</a>
<a class="pr-page-next">Next »</a>


$('.pr-page-prev').text().replace(/&laquo;/, '')
$('.pr-page-next').text().replace(/&raquo;/, '')

DEMO

Hi Am trying to remove the arrows from JS. but seems like replace is not working :-(

<a class="pr-page-prev">« Previous</a>
<a class="pr-page-next">Next »</a>


$('.pr-page-prev').text().replace(/&laquo;/, '')
$('.pr-page-next').text().replace(/&raquo;/, '')
Share Improve this question asked Mar 17, 2015 at 9:33 DeveloperDeveloper 1,4373 gold badges23 silver badges48 bronze badges 2
  • 3 There is no &laquo; in the text you are getting it's the actual symbol: «, have you tried replacing that? Also you are doing nothing with the result: jsfiddle/peteng/t1rru99a/3 – Pete Commented Mar 17, 2015 at 9:35
  • possible duplicate of jquery text().replace('','') not working and many others – JJJ Commented Mar 17, 2015 at 9:40
Add a ment  | 

5 Answers 5

Reset to default 9

You are replacing the text but not the content of the element.

When you say $('.pr-page-prev').text(), it returns the text content on which you are calling replace which will return the replaced text but it is not set back to the element. So even if you have replaced the text it won't be applied to the element.

$('.pr-page-prev').text(function(i, text){return text.replace(/«/, '')})
$('.pr-page-next').text(function(i, text){return text.replace(/»/, '')})

Demo: Fiddle

That's because you are not doing anything with the results of the replace:

e.g.

$('.pr-page-prev').text($('.pr-page-prev').text().replace(/«/, ''));

Or using a function instead of the value:

$('.pr-page-prev').text(function(){ return $(this).text().replace(/«/, ''));

http://jsfiddle/TrueBlueAussie/t1rru99a/6/

Or better yet, bine them both into a single operation (the Regex shown will match either character):

$('.pr-page-prev,.pr-page-next').text(function () {
    return $(this).text().replace(/[«»]/, '')
});

JSFiddle: http://jsfiddle/TrueBlueAussie/t1rru99a/9/

You can replace the value "«" with "space".

$(".pr-page-prev").text(function(i, val) {
    return val.replace(/«/g, " ");
});
$(".pr-page-next").text(function(i, val) {
    return val.replace(/»/g, " ");
});

Working Demo

You're replacing the string as a result but not changing what is displayed... what you need to do is to use the text() method to get and to set the data, here's an example :

$('.pr-page-prev').text($('.pr-page-prev').text().replace("«", ''))

The problem is that you are trying to access/read the text but not write it back. To write it back you need to replace the text as well. You can do it by JSFiddle.

$('.pr-page-prev').text($('.pr-page-prev').text().replace("«", ''))
$('.pr-page-next').text($('.pr-page-next').text().replace("»", ''))
发布评论

评论列表(0)

  1. 暂无评论