将数据从一个HTML文件传输到另一个 [英] Transfer data from one HTML file to another
问题描述
我是HTML和Javascript的新手,我试图做的是从一个HTML
文件,我想提取那些设置的东西并将其显示给另一个HTML
文件通过 Javascript
以下是我所做的到目前为止测试它:
testing.html
< html> ;
< head>
< script language =javascripttype =text / javascriptsrc =asd.js>< / script>
< / head>
< body>
< form name =form1action =next.htmlmethod =get>
名称:< input type =textid =namename =n>
< input type =submitvalue =next>
< button type =buttonid =printonClick =testJS()>打印< /按钮>
< / form>
< / body>
< / html>
next.html
< head>
< script language =javascripttype =text / javascriptsrc =asd.js>< / script>
< / head>
< body>
< form name =form1action =next.htmlmethod =get>
< table>
< tr>
< td id =here> test< / td>
< / tr>
< / table>
< / form>
< / body>
< / html>
asd.js
函数testJS()
{
var b = document.getElementById('name')。value
document.getElementById('here')。innerHTML = b;
$ b
test.html
- > ads.js
(将从test.html提取值并设置为next.html) - > next.html
试试这段代码:
在testing.html中
function testJS(){
var b = document.getElementById('name')。value,
url ='http:/ /path_to_your_html_files/next.html?name='+ encodeURIComponent(b);
document.location.href = url;
}
在next.html中:
window.onload = function(){
var url = document.location.href,
params = url.split('?')[ 1] .split('&'),
data = {},tmp;
for(var i = 0,l = params.length; i tmp = params [i] .split('=');
data [tmp [0]] = tmp [1];
}
document.getElementById('here')。innerHTML = data.name;
}
描述:javascript不能在不同页面之间共享数据,我们必须使用一些解决方案,例如URL获取参数(在我的代码中,我用这种方式),Cookie,localStorage等
将名称参数存储在URL(?name = ...)和next.html解析URL中,并从prev获取所有参数页面。
PS。我是非母语的人,如果有必要,请你纠正我的信息。
I'm new to HTML and Javascript, what i'm trying to do is from an HTML
file i want to extract the things that set there and display it to another HTML
file through Javascript
Here's what i done so far to test it:
testing.html
<html>
<head>
<script language="javascript" type="text/javascript" src="asd.js"></script>
</head>
<body>
<form name="form1" action="next.html" method="get">
name:<input type ="text" id="name" name="n">
<input type="submit" value="next" >
<button type="button" id="print" onClick="testJS()"> Print </button>
</form>
</body>
</html>
next.html
<head>
<script language="javascript" type="text/javascript" src="asd.js"></script>
</head>
<body>
<form name="form1" action="next.html" method="get">
<table>
<tr>
<td id="here">test</td>
</tr>
</table>
</form>
</body>
</html>
asd.js
function testJS()
{
var b = document.getElementById('name').value
document.getElementById('here').innerHTML = b;
}
test.html
-> ads.js
(will extract value from the test.html and set to next.html) -> next.html
Try this code: In testing.html
function testJS() {
var b = document.getElementById('name').value,
url = 'http://path_to_your_html_files/next.html?name=' + encodeURIComponent(b);
document.location.href = url;
}
And in next.html:
window.onload = function () {
var url = document.location.href,
params = url.split('?')[1].split('&'),
data = {}, tmp;
for (var i = 0, l = params.length; i < l; i++) {
tmp = params[i].split('=');
data[tmp[0]] = tmp[1];
}
document.getElementById('here').innerHTML = data.name;
}
Description: javascript can't share data between different pages, and we must to use some solutions, e.g. URL get params (in my code i used this way), cookies, localStorage, etc. Store the name parameter in URL (?name=...) and in next.html parse URL and get all params from prev page.
PS. i'm an non-native speaker, will you please correct my message, if necessary
这篇关于将数据从一个HTML文件传输到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!