使用javascript变量更改html中的特定字符串
change a specific string inside html with a javascript variable
我知道这里有类似我的问题,但遗憾的是,它们似乎都不起作用,无论如何,这就是我的情况;
我有多个特定的html字符串,我会用一个变量来更改
var:
var modelstring="something";
HTML:
<div class="caratteristichedesk" style="position: relative; text-align: center;font-family: 'corporate_s_reg';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>
我正在寻找一种非常简单的方法来用上面的var更改"MODELLO"字符串,有人有建议吗?感谢
这里有一个不用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: 'corporate_s_reg';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>
有很多方法可以做到这一点,
var modelstring ="something";
$(document).ready ( function (){
$('div').each( function() {
var txt = $(this).text();
$(this).text( txt.replace("MODELLO", modelstring) );
});
});
相关文章:
- 如何使用JQuery在Javascript中转换字符串中的HTML元素
- 如何使用JQuery在HTML中创建包含字符串参数的引号的onclickjavascript链接
- 如何使用gump任务将html转换为javascript字符串
- 将html元素插入到文本字符串中,以匹配另一个html字符串
- HTML字符串作为上下文
- 字符串中的
标记未正确在 HTML 中创建新行 - 将HTML(支持文件)中的令牌(字符串、数字等)传递给LogiXML
- 如何使用javascript选择字符串的部分,添加html标记或删除部分
- Visual Studio 2015 扩展将 JavaScript 文件中的字符串 HTML 视为 HTML 编辑
- 如何使用jquery插入多行字符串html
- 创建一个通过页面的查询字符串HTML表单
- PayPal确认,然后返回一个字符串HTML(js或任何其他..)
- 使用Javascript DOMParser将单字符串HTML格式化为多行选项卡HTML
- 如何转义字符串(HTML)中的多个层次引号
- 解析笑脸与文本字符串html和保持笑脸在显示顺序
- Regex帮助,JS尝试将字符串(html)转换为数字
- removeAttr在字符串html
- jqgrid asp.net-Javascript字符串HTML合成
- 如何用另一个标记替换字符串HTML标记
- 将变量追加到字符串html中