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

javascript - jQuery Replace character and symbol in string in each div , comma by € and € by nothing - Stack Overflow

programmeradmin4浏览0评论

so here is the scenario: I have a bunch of spans with a class of " price " like that : 12,99€

I needed to split the cents after the a so I could style it with css -> that's done , cents got its own class " cents "

Now I'm trying to replace the a "," by the € euro symbol, and remove the € symbol at the end 'replace it with nothing, empty)

I hope it makes sense, here is an example...:

How it is now: 12,99€ and What I need: 12€99

My code right now:

<!-- the html part CANNOT BE MODIFIED -->
<span class="price">29,49€</span>

<script>
jQuery(".price").each(function() {
var newText = jQuery(this).text().split(",").join("</span>,<sup class='cents'>");
newText = "<span class='euros'>" + newText + "</sup>";
jQuery(this).html(newText);
});

// Tryed to replace euro symbol by nothing but doesn't work //
var value = jQuery(".cents").val()+"";
value.replace("€", "");
var val = jQuery(".cents").val();
</script>

I would definitely appreciate if someone could help me out here since I really want to learn how to achieve this. It doesn't look too plicated but can't make it work which is frustating.

so here is the scenario: I have a bunch of spans with a class of " price " like that : 12,99€

I needed to split the cents after the a so I could style it with css -> that's done , cents got its own class " cents "

Now I'm trying to replace the a "," by the € euro symbol, and remove the € symbol at the end 'replace it with nothing, empty)

I hope it makes sense, here is an example...:

How it is now: 12,99€ and What I need: 12€99

My code right now:

<!-- the html part CANNOT BE MODIFIED -->
<span class="price">29,49€</span>

<script>
jQuery(".price").each(function() {
var newText = jQuery(this).text().split(",").join("</span>,<sup class='cents'>");
newText = "<span class='euros'>" + newText + "</sup>";
jQuery(this).html(newText);
});

// Tryed to replace euro symbol by nothing but doesn't work //
var value = jQuery(".cents").val()+"";
value.replace("€", "");
var val = jQuery(".cents").val();
</script>

I would definitely appreciate if someone could help me out here since I really want to learn how to achieve this. It doesn't look too plicated but can't make it work which is frustating.

Share Improve this question edited Jul 17, 2012 at 19:56 mlclm asked Jul 17, 2012 at 19:54 mlclmmlclm 7256 gold badges17 silver badges39 bronze badges 1
  • .val() is for form elements, you should use .text() instead. – kapa Commented Jul 17, 2012 at 20:00
Add a ment  | 

3 Answers 3

Reset to default 3
$(".price").text( function(i, oldtext){
    return oldtext.replace("€","").replace(",","€")
});

http://jsfiddle/8da54/9/

How about using the following instead:

$(".price").html(function(i, val) {
    val = val.split(",");
    return "<span class='euros'>" + val[0] + "</span>" + "€" +
        "<sup class='cents'>" + val[1].replace("€", "") + "</sup>";
});

DEMO: http://jsfiddle/8da54/11/

You don't need to use replace, you can use slice or substr.

$(".price").html(function(i, oldVal) {
    var value = oldVal.split(",");
    return "<span class='euros'>" + value[0] + "€</span>" +
        "<sup class='cents'>" + value[1].slice(0, -1) + "</sup>";
});

demo

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论