Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.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 JS替换变量中html中的id_Javascript_Html_Replace - Fatal编程技术网

Javascript JS替换变量中html中的id

Javascript JS替换变量中html中的id,javascript,html,replace,Javascript,Html,Replace,我创建了一个javascript,它将html片段注入index.php文件中。它需要将id替换为一个新id,其中包含一个索引号。 这是my type.txt: <section id="banner" class="wow fadeInUp" style="position: relative; padding: 100px 0;"> <div class="container"> <div class="row">

我创建了一个javascript,它将html片段注入index.php文件中。它需要将id替换为一个新id,其中包含一个索引号。 这是my type.txt:

<section id="banner" class="wow fadeInUp" style="position: relative; padding: 100px 0;">
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-1 col-sm-6">
                <div class="block" style="padding-top: 10px;">
                    <h1 id="K0title" style="margin-bottom: 18px; line-height: 55px; color: #262b31; font-weight: 300;">
                        Loading...
                    </h1>
                    <p id="K0text" style="margin-bottom: 65px; font-size: 20px; color: #999; font-weight: 300;">
                        Loading...
                    </p>

                </div>
            </div>
            <div class="col-md-4 col-sm-6">
                <div class="block" style="padding-top: 10px;">
                    <img id="K0image" class="app-img img-responsive" src="images/background 2.png" alt="">
                </div>

            </div>
        </div>
    </div>
</section>
不知怎的,id保持K0。。。在index.php中

我认为这与“”有关

thx

if(type == 1){
        var html1 = new XMLHttpRequest();
        html1.open('GET', 'js/txt/type1.txt', false);
        html1.send();
        allText = html1.responseText;
        titleReplace = "K" + id + "title";
        textReplace = "K" + id + "text";
        imageReplace = "K" + id + "image";
        alert(titleReplace);
        allText.replace("K0title", titleReplace);
        allText.replace("K0text", textReplace);
        allText.replace("K0image", imageReplace);
        document.getElementById(id).innerHTML = allText;
        document.getElementById(titleReplace).innerHTML = title;
        document.getElementById(textReplace).innerHTML = text;
        document.getElementById(imageReplace).src = imageUrl;
    }
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");