I am trying to change the color of elements that are only within the <code>
tag but does not work.
I dont want to use document.GetElementByID as per the answers here getElementsByTagName in JavaScript
Below is my original code.
<div id="wmd-post">
<code>
<p></span></p> <!-- i would want only this to change color -->
</code>
<p></span></p> <!-- i would NOT want this to change color -->
</div>
<script>
var text = document.getElementById("wmd-post");
var str = text.innerHTML;
str = str.replace(/<\/span>/g, '<span style="color:red"></span></span>');
document.getElementById("wmd-post").innerHTML = str;
</script>
What i have tried is
<div id="wmd-post">
<code>
<p></span></p>
</code>
<p></span></p>
</div>
<script>
var text = document.getElementsByTagName('code');
var str = text.innerHTML;
for(var i = 0; i < str.length; i++)
{
str[i] = str.replace(/<\/span>/g, '<span style="color:red"></span></span>');
document.getElementsByTagName("code").innerHTML = str[i];
}
</script>
答案 0 :(得分:1)
I still don't know why you're not using css, but here goes:
html:
<div id="wmd-post">
<code>
<p></span></p>
</code>
<p></span></p>
</div>
JS:
var text = document.getElementsByTagName('code')[0];
var str = text.innerHTML;
str = str.replace(/<\/span>/g, '<span style="color:red"></span></span>');
document.getElementsByTagName("code")[0].innerHTML = str;
Fiddle: https://jsfiddle.net/u8ystwyg/1/
A css only solution:
<style>
code { color: red; }
</style>
<div id="wmd-post">
<code>
<p></span></p>
</code>
<p></span></p>
</div>
答案 1 :(得分:1)
Please check this fiddle Click Here
You just have to change a Script of your code , Here is the script.
var text = document.getElementsByTagName('code');
var str = text[0].innerHTML;
str = str.replace(/<\/span>/g, '<span style="color:red"></span></span>');
document.getElementsByTagName("code")[0].innerHTML = str;
I hope this is helpful to you.
答案 2 :(得分:1)
Your question had the jQuery tag before it was edited away by another user. In case you want to use jQuery, here is one solution.
$(document).ready(function() {
$("#wmd-post > code").css("color", "red");
});
Check out the JSFiddle
答案 3 :(得分:0)
document.getElementsByTagName
returns a NodeList
, not a single element. You need to loop over the list.
var codes = document.getElementsByTagName('code');
for (var i = 0; i < codes.length; i++) {
var str = codes[i].innerHTML;
str = str.replace(/<\/span>/g, '<span style="color:red"></span></span>');
codes[i].innerHTML = str;
}
<div id="wmd-post">
<code>
<p></span></p>
</code>
<p></span></p>
</div>
答案 4 :(得分:0)
Here a working example using Element.getElementsByTagName() which basically gets all elements in the document with the specified tag (in your case code
tag). You can replace the "target" string using String.prototype.replace() .
(function(){
var text = document.getElementsByTagName('code'),
str = text[0].innerHTML;
str = str.replace(/<\/span>/g, '<span class="red"></span></span>');
text[0].innerHTML = str;
})();
.red{
color:red;
}
<div id="wmd-post">
<code>
<p></span></p>
</code>
<p></span></p>
</div>