2016-10-09 39 views
1

我創建了一個javascript,它在index.php文件中注入html片段。它需要用一個帶有索引號的新ID來替換id。 這是我type.txt:JS從var替換id中的id

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

這一塊我的javascript:

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

不知何故,ID將保持K0 ......在index.php。

我認爲這是有事情做的「」

THX

回答

2
var str = "Visit Microsoft!"; 
var res = str.replace("Microsoft", "W3Schools"); 
+0

哦,我只是我以前不看它... –

+0

那麼愚蠢:),而是爲了幫助THX –