2017-05-07 60 views
0

我有兩個HTML文件:index.htmlPage2.html
我在index.html頁面有一個按鈕,名爲WWW_onclick()
如您所見,此功能打開Page2.html並寫入「hello」。
問題是這個功能抹去了原來的Page2.html,只寫「你好」。
JavaScript刪除第二頁的內容

有沒有辦法保留Page2.html的內容並在其中添加其他文字,圖片或...?

function WWW_onclick() 
{ 
    document.location.href = "Page2.html"; 
    document.write("hello"); 
} 
+0

重複:http://stackoverflow.com/questions/9280750/how-to-change-content-after-page-is-loaded- js – Quentin

回答

0

看看document.write

它會清除你的HTML。

您應該使用innerHTML

例子:

document.getElementById("your-id").innerHTML = "hello"; 
+0

我試過這個,但它只打開了page2 $(document).ready(function(){(「#btn1」)。click(function(){ document.location.href =「Page2.html」; document.getElementById(「par」)。innerHTML =「hello」; }); }); – HaNas

+0

@HamidNasr編輯您的OP並告訴我們您嘗試了什麼。 – Edward