两个html页面之间怎样传参数
1个回答
展开全部
html是静态页面,可以使用url链接传值,比如a.html和b.html两个页面
a.html中有一个链接
1
<a href="b.html?x=2&y=3">进入b.html</a>
可以使用到js,如下:
a.htm:
<form action="b.htm" >
<input name="q" type="text" value="" />
<input type="submit" value="提交" id="" />
</form>
b.htm
<html>
<body>
<div id="qbox"></div>
<script type="text/javascript">
function getArgs() {
var args = {};
var query = location.search.substring(1);
// Get query string
var pairs = query.split("&");
// Break at ampersand
for(var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexOf('=');
// Look for "name=value"
if (pos == -1) continue;
// If not found, skip
var argname = pairs[i].substring(0,pos);// Extract the name
var value = pairs[i].substring(pos+1);// Extract the value
value = decodeURIComponent(value);// Decode it, if needed
args[argname] = value;
// Store as a property
}
return args;// Return the object
}
var str =getArgs();
alert(str['q']);//和input的name对应取值,
document.getElementById("qbox").innerHTML = str['q'];//然后赋值给DIV
</script>
</body>
</html>
a.html中有一个链接
1
<a href="b.html?x=2&y=3">进入b.html</a>
可以使用到js,如下:
a.htm:
<form action="b.htm" >
<input name="q" type="text" value="" />
<input type="submit" value="提交" id="" />
</form>
b.htm
<html>
<body>
<div id="qbox"></div>
<script type="text/javascript">
function getArgs() {
var args = {};
var query = location.search.substring(1);
// Get query string
var pairs = query.split("&");
// Break at ampersand
for(var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexOf('=');
// Look for "name=value"
if (pos == -1) continue;
// If not found, skip
var argname = pairs[i].substring(0,pos);// Extract the name
var value = pairs[i].substring(pos+1);// Extract the value
value = decodeURIComponent(value);// Decode it, if needed
args[argname] = value;
// Store as a property
}
return args;// Return the object
}
var str =getArgs();
alert(str['q']);//和input的name对应取值,
document.getElementById("qbox").innerHTML = str['q'];//然后赋值给DIV
</script>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询