jQuery替换特定的文本

时间:2012-01-11 06:48:43

标签: javascript jquery text replace

简单地说,如何使用jQuery用HTML替换精确文本?

原始文字是 View All Customer Reviews

我希望将其替换为<span class="redbutton">View All Reviews &#187;</span>

感谢。

2 个答案:

答案 0 :(得分:2)

<div id="_div">xxx|||View All Customer Reviews|||xxx</div>
<script>
$(function() {
var html = $('#_div').html() + '';
html = html.replace(/View All Customer Reviews/g, '<span class="redbutton">View All Reviews &#187;</span>');
$('#_div').html(html);
});
</script>

// =================== ---- ================== /// < / p>

<a>xxx|||View All Customer Reviews|||xxx</a>
<a>yyy|||View All Customer Reviews|||yyy</a>
<a>zzz|||View All Customer Reviews|||zzz</a>
<script>
$(function() {
    $('a').each(function(i) {
        var html = $(this).html() + '';
        html = html.replace(/View All Customer Reviews/g, '<span class="redbutton">View All Reviews &#187;</span>');
        $(this).html(html);
    });
});
</script>

答案 1 :(得分:0)