Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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 替换方法不在浏览器中工作,但在console.log中工作_Javascript_Wordpress_Browser_Replace_Console.log - Fatal编程技术网

Javascript 替换方法不在浏览器中工作,但在console.log中工作

Javascript 替换方法不在浏览器中工作,但在console.log中工作,javascript,wordpress,browser,replace,console.log,Javascript,Wordpress,Browser,Replace,Console.log,我正在尝试替换Wordpress网站中出现的多个内容,下面是我所做的: window.onload = init; function init() { // get all the divs which have specific class names let divElems = document.querySelectorAll(".vc_custom_heading.white.vc_gitem-post-data.vc_gitem-post-data-sou

我正在尝试替换Wordpress网站中出现的多个内容,下面是我所做的:

window.onload = init;

function init()
{
    // get all the divs which have specific class names
    let divElems = 
    document.querySelectorAll(".vc_custom_heading.white.vc_gitem-post-data.vc_gitem-post-data-source-post_title");

    // loop on the divs
    for(let i = 0 ; i < divElems.length ; i++)
    {
        // get the first child of the div : a <h2>
        let titleElem = divElems[i].childNodes[0];
        // get the first child of the title : a <a>
        let linkElem  = titleElem.childNodes[0];
        // get the text content of the link
        let text      = linkElem.textContent;

        // replace the word test by nothing
        text = text.replace(/test/g, '');
        // all the occurrences of test have been removed in the console.log, but not in browser
        console.log(text);
    }
};
window.onload=init;
函数init()
{
//获取所有具有特定类名的div
设divElems=
document.queryselectoral(“.vc_custom_heading.white.vc_gitem-post-data.vc_gitem-post-data-source-post_title”);
//在沙发上打圈
for(设i=0;i
但奇怪的是,replace方法在控制台.log中运行良好(我可以看到测试词已被删除),但在浏览器页面中没有任何更改

有人有主意吗?:)


ArbreMojo。

您只是更新变量值,因此它不会更新实际的元素内容。要使其工作,只需更新属性即可更新元素内容

text = text.replace(/test/g, '');
linkElem.textContent = text;

@阿布雷莫乔:很高兴这有帮助