替换标签内的第一个字符

时间:2012-04-26 16:32:50

标签: javascript jquery

如何使用jQuery将除最后一个标签字符$ sign之外的每个标签首字符更改为£?

以下是代码:

<p align="center" id="myradio">
    <label>$25.00 </label>
    <input checked="checked" class="hide_input" name="amount" type="radio" value="25.00" />
    <label>$50.00</label>
    <input class="hide_input" name="amount" type="radio" value="50.00" />
    <label>$100.00</label>
    <input class="hide_input" name="amount" type="radio" value="100.00" />
    <label>$250.00</label>
    <input class="hide_input" name="amount" type="radio" value="250.00" />
    <label>$500.00</label>
    <input class="hide_input" name="amount" type="radio" value="500.00" />
    <label>$1000.00</label>
    <input class="hide_input" name="amount" type="radio" value="1000.00" />
    <label>other amount</label>
    <input class="show_input" id="other" name="amount" type="radio" value="" />
</p>

4 个答案:

答案 0 :(得分:12)

您可以使用内部循环来交换字符:

$("#myradio label").html(function(){
    return this.innerHTML.replace( '$', '£' ); 
});

答案 1 :(得分:3)

$("#myradio label:not(:last)").each(function() {
    $(this).text($(this).text().replace(/^\$/, "£"));
});

答案 2 :(得分:2)

    $('#myradio label').each(function() {
       $(this).text('£' + $(this).text().substr(1));
    });

这基本上采用标签的子串,从第一个字符开始(直到结尾) - 这是没有$符号的文本。并将其附加到英镑符号上。此连接文本用于替换现有文本。

答案 3 :(得分:1)

$('#myradio label').html(function() {
   return this.innerHTML.replace(/^\$/,'£');
});