2016-12-03 68 views
0

我想從另一個頁面訪問HTML頁面的元素ID。我已經看到其他的解決方案,但目前爲止還沒有工作。如何使用JavaScript從另一個頁面訪問元素ID?

例如,如果這是One.html

<input id="a" type="text"></input> 
<p id="demo"></p> 

我試圖訪問該元素的id「是一種」自Two.html讓已使用輸入的值`

var thing = document.getElementById('a').value; 
document.getElementById('demo').innerHTML = "the value is" + " " + thing; 
+0

不是從您當前的網頁? –

+0

只有在非常有限的條件下,網頁可以相互交談,所以我們需要更多的細節,才能幫助您 – Bindrid

+0

是否有兩次HTML文件加載到您的瀏覽器? –

回答

1
you can use localStorage 
for example in firstpage.html you save data with 
var thing = document.getElementById('a').value; 
localStorage.something = thing ; 
then in your seconpage.html you can use it by 
document.getElementById("demo").innerHTML=localStorage.something; 
+0

我要在第一個或第二個HTML頁面中添加這個嗎?如果它是第一頁,那麼我如何從第二頁訪問本地存儲? –

+0

它膨脹! –

0

對不起,我沒有足夠的聲望留下這個評論,但只是一個頭,輸入標籤是獨立的標籤。

Example: 
    <body> 
    <input type="text" id="a" /> 
    </body> 
+0

謝謝!我不知道! –

相關問題