我的WordPress网站上有HTML,看起来像这样:
<div id="content" class="clearfix">
<header class="page-header">
<h1 class="page-title">
<span>
Archives
</span>
</h1>
</header><!-- .page-header -->
<div class="article-container">
如何将字符串“ Archives”替换为“ test string”?
这是我到目前为止的内容,但是不起作用。
<?php
?>
<script>
var spans = document.getElementsByTagName("span");
for (var i=0; i < spans.length; i++)
{
if (spans[i].innerHTML.contains("Archives"))
{
//is this the "Welcome" span?
spans[i].innerHTML = "test string"; //change to new value
break; //hop out of the loop, we're done
}
}
</script>
最佳答案
做这样的事情
<script>
function changeme(){
var spans = document.getElementsByTagName("span");
for(var i=0;i<spans.length; i++) {
if(spans[i].innerHTML.trim().toLowerCase()==="archives") { //is this the "Welcome" span?
spans[i].innerHTML = "test string"; //change to new value
break; //hop out of the loop, we're done
}
}
}
window.onload = function() {
changeme();
};
</script>