2014-10-07 17 views
0

我正在創建一個包含5個不同頁面的網站,但每個頁面上的標題必須相同。我需要使用外部JavaScript文件(.js)來執行此操作。標題包含網站名稱(作爲圖像)以及導航欄。使用Javascript在網頁上覆制標題

我不知道該從哪裏開始。我試圖創建一個函數並將信息寫入文檔(使用document.write),但這似乎不起作用。我是否以錯誤的方式去做?我怎樣才能使它工作?

Html code that I need to be repeated: 

<header> 
     <img id = "name" src = "StelluxStay.png" alt = "StelluxStay" /> 

     <nav class = "horizontalNAV"> 
      <ul> 
       <li id = "current"> <a href = "Home.html"> Home </a> </li> 
       <li> <a href = "#"> Villas </a> </li> 
       <li> <a href = "#"> Facilities </a> </li> 
       <li> <a href = "#"> Kids </a> </li> 
       <li> <a href = "#"> Contact Us </a> </li> 
      </ul> 
     </nav> 

     <hr>  
</header> 

The javascript function that I have tried to create in an external .js sheet: 

function repHeader() { 
document.write (
<header> + 
    <img id = "name" src = "StelluxStay.png" alt = "StelluxStay" /> + 

    <nav class = "horizontalNAV"> + 
     <ul> + 
      <li id = "current"> <a href = "Home.html"> Home </a> </li> + 
      <li> <a href = "#"> Villas </a> </li> + 
      <li> <a href = "#"> Facilities </a> </li> + 
      <li> <a href = "#"> Kids </a> </li> + 
      <li> <a href = "#"> Contact Us </a> </li> + 
     </ul> + 
    </nav> + 

    <hr> + 
</header>) ; 
} 


I then call the function in the Html page. 
<script type = "text/javascript"> 
     repHeader() ; //call function 
</script> 

回答

0

你只需要報價

document.write("<header><img id = 'name' src = 'StelluxStay.png' alt = 'StelluxStay' /><nav class = 'horizontalNAV'><ul><li id = 'current'> <a href = 'Home.html'> Home </a> </li><li> <a href = '#'> Villas </a> </li><li> <a href = '#'> Facilities </a> </li><li> <a href = '#'> Kids </a> </li><li> <a href = '#'> Contact Us </a> </li> </ul> </nav><hr> </header>"); 
+0

感謝。我改變了引號,但它仍然不起作用。 – Emj 2014-10-07 11:10:39

+1

等一下,我已經準備好了。非常感謝! – Emj 2014-10-07 11:14:56

相關問題