使用javascript变量更改html中的特定字符串

时间:2015-11-27 10:59:20

标签: javascript string replace

我知道这里有像我这样的类似问题,但遗憾的是他们似乎没有工作,无论如何这是我的情况;

我有多个特定的html字符串,我会用变量

更改

var:

        <div class="caratteristichedesk" style="position: relative; text-align: center;font-family: &#39;corporate_s_reg&#39;;height:200px">
            <img class="pointdesk" src="img/pointers.png">
            <div style="position:absolute;top:60px;width:200px;left:5px;">Verrai ricontattato entro 24 ore per confermare i tuoi dati.</div>
            <div style="position:absolute;top:60px;width:200px;left:235px;">Il dealer ti richiamerà per confermare l’appuntamento.</div>
            <div style="position:absolute;top:60px;width:250px;left:435px;">Vivi l’esperienza di guida a bordo <br>della MODELLO.</div>
        </div>

HTML:

{{1}}

我正在寻找一种非常简单的方法来改变&#34; MODELLO&#34;我的var上面的字符串,有人有建议吗?谢谢

2 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点,

var modelstring ="something";
$(document).ready ( function (){ 
    $('div').each( function() { 
        var txt = $(this).text();
       $(this).text( txt.replace("MODELLO", modelstring) );
    });  
});

答案 1 :(得分:0)

这是一个免费的jquery方法。

var modelstring = "something",
    replaceSelector = document.querySelectorAll('.toReplace'),
    replaceCount;

for(replaceCount = 0; replaceCount < replaceSelector.length; replaceCount++) {
    replaceSelector[replaceCount].innerHTML = modelstring;
}
<div class="caratteristichedesk" style="position: relative; text-align: center;font-family: &#39;corporate_s_reg&#39;;height:200px">
    <img class="pointdesk" src="img/pointers.png">
    <div style="position:absolute;top:60px;width:200px;left:5px;">Verrai ricontattato entro 24 ore per confermare i tuoi dati.</div>
    <div style="position:absolute;top:60px;width:200px;left:235px;">Il dealer ti richiamerà per confermare l’appuntamento.</div>
    <div style="position:absolute;top:60px;width:250px;left:435px;">Vivi l’esperienza di guida a bordo
        <br>della <span class="toReplace">MODELLO.</span></div>
</div>