[英]javascript - Set color of characters from one to another
Using Javascript, I want to construct myFunction()
such that this HTML code: 我想使用Javascript构造myFunction()
,以便此HTML代码:
<body onload="myFunction();">
Hello world! This is "'my' webpage"!<br>
Do you know what "Javascript" is?
</body>
will be converted to this HTML code: 将被转换为以下HTML代码:
<body onload="myFunction();">
Hello world! This is <a style="color: blue;">"'my' webpage"</a>!<br>
Do you know what <a style="color: blue;">"Javascript"</a> is?
</body>
So in other words, I want all characters between (double) inverted commas ""
to be blue. 因此,换句话说,我希望(双)反逗号""
之间的所有字符都是蓝色。
I tried this: 我尝试了这个:
function myFunction() {
var body_content = document.getElementsByTagName("body")[0].innerHTML;
document.getElementsByTagName("body")[0].innerHTML =
body_content.replace(/\"/g, '<a style="color: Blue;">"');
}
But it made everything blue after the first double inverted comma in the body. 但是,在体内第一个双反逗号后,一切都变成了蓝色。
How do I construct myFunction()
? 如何构造myFunction()
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.