简体   繁体   English

javascript .replace html标签不会替换

[英]javascript .replace a html tag does not replace

I am trying to replace some html but the replace is not taking place. 我正在尝试替换一些html,但是替换没有发生。 I am using the javascript .replace and from what I can see I am using it right. 我正在使用javascript .replace ,从中可以看到我正在正确使用它。

content.replace('<link rel="stylesheet" href="style.css">', '<style>html{background-color: blue;}<\/style>');

The strings replace() method returns a new string with the new value. 字符串replace()方法返回具有新值的新字符串。 It doesn't modify the original string. 不会修改原始字符串。

Here is a simple example: 这是一个简单的示例:

var str = "Hello World!";
var res = str.replace("World", "Foo");

console.log(str);  // prints Hello World!

应该这样做:

$('[href="style.css"]').prepend("html{background-color: blue;}");

As stated the replace doesn't modify the original string. 如前所述,替换不会修改原始字符串。

Try the below example, 试试下面的例子,

document.getElementById("demo").innerHTML = "Paragraph changed!";

Info here 这里的信息

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

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