2016-07-18 4 views
0

Hello 최근에 나는 HTML 작업을 시작했는데, iam은 HTML 페이지 하나에서 다음 html 페이지로 값을 보내고 싶은 상황에서 우리의 이름은 후하나의 HTML 페이지에서 다른 페이지로 javascript 변수 전달하기

pageOne.html

<!DOCTYPE html> 
<head> 
<title> 
Home 
</title> 
<meta charset="UTF-8"> 
</head> 
<style> 
.myP{ 
color: lightgreen;} 
</style> 
<body> 
<p id="p1" class="myP" onclick="myFun()">DataSend</p> 
</body> 
<script> 
function myFun(){ 
document.getElementById("p1").style.color = "blue"; 
var textprevi=document.getElementById("p1").innerHTML; 
localStorage.setItem("message", textprevi); 
window.open("pageTwo.html","_self"); 
} 
</script> 
</html> 

, 내가 두 HTML 페이지를 작성했습니다 success.Here을 가입하고 내 두 번째

제가 상기 용액, ID = " TBOX"인 요소를 시도 할 때 16,

pageTwo.html

<!DOCTYPE html> 
<html> 
<body onload="fun"> 
<input type="text" id="tBox"> 
</body> 
<script> 
function fun() 
{ 
document.getElementById("tBox").innerHTML = localStorage.getItem("message"); 
} 

</script> 
</html> 

하지만 비어 있지만 그것이 내지 값 = "DataSend"채워질 싶었 pageOne.html. 제발 도와주세요. 미리 감사드립니다.

답변

1

문제는이 라인 여기

document.getElementById("tBox").innerHTML = localStorage.getItem("message"); 

tBox하는 것입니다 입력 요소. 그래서 대신 당신은 pageTwo.html에 두 개의 실수를 innerHTML

document.getElementById("tBox").value= localStorage.getItem("message"); 
0

당신은 1 페이지에서 쿼리 문자열 매개 변수 를 사용하여 수행 할 수 있습니다

이 페이지에서
function myFun(){ 
    document.getElementById("p1").style.color = "blue"; 
    var textprevi=document.getElementById("p1").innerHTML; 
    window.open("pageTwo.html?data=" + textprevi,"_self"); 
} 

2

function getParameterByName(name, url) { 
    if (!url) url = window.location.href; 
    name = name.replace(/[\[\]]/g, "\\$&"); 
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), 
     results = regex.exec(url); 
    if (!results) return null; 
    if (!results[2]) return ''; 
    return decodeURIComponent(results[2].replace(/\+/g, " ")); 
} 

var data = getParameterByName('data'); 
0

value를 사용해야합니다.

<!DOCTYPE html> 
<html> 
<body onload="fun()"><!-- You didn't invoke the function here --> 
<input type="text" id="tBox"> 
</body> 
<script> 
function fun() 
{ 
document.getElementById("tBox").value = localStorage.getItem("message"); // You need to set the value for input fields, not innerHTML. 
} 

</script> 
</html> 

이러한 변경으로 인해 코드가 작동합니다.

관련 문제