简体   繁体   English

如何用html替换文本

[英]How to replace text with html

I am trying to replace a phone number with a new phone number.我正在尝试用新电话号码替换电话号码。
However, it's not working with the <p> tags right now.但是,它现在不适用于<p>标签。

I can replace it with a word and it changes it.我可以用一个词代替它,它会改变它。 But when I use HTML it doesn't work但是当我使用 HTML 时它不起作用

<script>
$(function() {
$("#navbarPhoneNumber").html("<p id='phonenumber'></p>");
});
</script>

对于这个简单的例子,不要使用 jquery,只需使用 DOM 的简单 API 函数.getElementById

document.getElementById('navbarPhoneNumber').innerText = <new phone number>

You can use innerHTML after you get your element to replace your text您可以在获取元素后使用 innerHTML 替换文本


  
  document.getElementById("navphonenumber").innerHTML = phonenumber;

Here is a solution I wrote这是我写的一个解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM