Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将a<;的innerHTML作为目标;span>;在php中没有任何ID或标记的元素?_Javascript_Php_Jquery_Html_Wordpress - Fatal编程技术网

Javascript 如何将a<;的innerHTML作为目标;span>;在php中没有任何ID或标记的元素?

Javascript 如何将a<;的innerHTML作为目标;span>;在php中没有任何ID或标记的元素?,javascript,php,jquery,html,wordpress,Javascript,Php,Jquery,Html,Wordpress,我的WordPress网站上有如下HTML: <div id="content" class="clearfix"> <header class="page-header"> <h1 class="page-title"> <span> Archives </span> </h1> </header><!-- .page-header --&g

我的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">

档案室
如何用“测试字符串”替换字符串“存档”

这是我到目前为止所做的,但不起作用

<?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>

var span=document.getElementsByTagName(“span”);
对于(变量i=0;i
您可以通过以下方式使用
span
的父级:

document.querySelector('h1.page-title span').textContent='teststring'

档案室

看起来脚本是在加载任何HTML元素之前运行的。将
脚本
部分放在HTML的底部,例如,就在
标记的前面,或者将其放入一个函数中,在加载文档后调用,如

<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();">....

函数replaceValue(){
var span=document.getElementsByTagName(“span”);
对于(var i=0;i执行以下操作

<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>

函数changeme(){
var span=document.getElementsByTagName(“span”);

对于(var i=0;它存在于我的php文件中。所以我用这个替换了它。这很好,但是你假设他使用JQuery,虽然他尝试的时候没有JQuery代码,但是如果你也给出一个非JQuery的解决方案会更好。@D.Smania,我用JQuery回答,因为在问题中标记了这个问题……我也添加了JavaScript解决方案………thanks@Mamun,我知道被标记为JQuery,但他没有使用它,所以看起来(至少对我来说)他需要一个非JQuery的解决方案。现在看起来更好了,+1适合你。问题是我不能把它放在html的底部。每个存档都有自己的标记名,所以我想给它的标记名…(在页面标题中生成)而不是deatult“Archive”谢谢!这就成功了!是因为空格吗?是的,总是尝试删掉字符串外的空格,这样你可以使你的检查更清楚,并使用不区分大小写的搜索,还有可能是你的html更经常有空格,你不能根据你的要求过滤数据,happy编码:)您是否尝试将JS代码放入文档就绪函数中?