我的WordPress网站上有HTML,如下所示:
int[4][5]
如何将字符串“ Archives”替换为“ test string”?
这是我到目前为止的内容,但不起作用。
<div id="content" class="clearfix">
<header class="page-header">
<h1 class="page-title">
<span>
Archives
</span>
</h1>
</header><!-- .page-header -->
<div class="article-container">
答案 0 :(得分:1)
您可以按照以下方式使用span
的父级:
document.querySelector('h1.page-title span').textContent = 'test string';
<div id="content" class="clearfix">
<header class="page-header">
<h1 class="page-title">
<span>
Archives
</span>
</h1>
</header><!-- .page-header -->
<div class="article-container">
jQuery解决方案:
$('h1.page-title span:first').text('test string');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content" class="clearfix">
<header class="page-header">
<h1 class="page-title">
<span>
Archives
</span>
</h1>
</header><!-- .page-header -->
<div class="article-container">
答案 1 :(得分:0)
在加载任何HTML元素之前,脚本似乎正在运行。例如,可以将script
部分放在HTML的底部,例如,放在</body>
标签的前面,或者将其放入一个函数,这样在文档加载后便会被调用...
<script>
function replaceValue(){
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>
....
...
<body onLoad="replaceValue();">....
答案 2 :(得分:0)
做这样的事情
<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>