详解前端在html页⾯之间传递参数的⽅法
项⽬中经常会出现的⼀种情况,有⼀个列表,譬如是案例列表,点击列表中的某⼀项,跳转⾄详情页⾯。详情是根据所点击的某条记录⽣成的,因为案例和具体的详情页⾯,都是⽤户后期⾃⾏添加的,我们开始编写时,不可能穷尽。因此跳转页⾯时,我们需要传递⼀个参数过去,这样我们才能通过这个参数进⾏数据请求,然后根据后台返回的数据来⽣成页⾯。因此,通过a 标签跳转的⽅式,肯定是⾏不通的。
我们经常写form表单,提交时,可以传递参数,如果使⽤表单,并将其隐藏起来,应该可以达到效果。
除此以外,window.location.href和window.open也可以达到效果。
1、通过form表单传递参数
<html lang="en">
<head>
<!--⽹站编码格式,UTF-8 国际编码,GBK或 gb2312 中⽂编码-->
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="Keywords" content="关键词⼀,关键词⼆">
<meta name="Description" content="⽹站描述内容">
<meta name="Author" content="Yvette Lau">
<title>Document</title>
<!--css js ⽂件的引⼊-->
<!-- <link rel="shortcut icon" href="images/favicon.ico">        -->
<link rel="stylesheet" href=""/>
<script type = "text/javascript" src = "jquery-1.11.2.min.js"></script>
</head>
<body>
<form name = "frm" method = "get" action = "receive.html" onsubmit = "return foo()" style = "position:relative;">
<input type="hidden"  name="hid" value = "" index = "lemon">
<img class = "more" src = "main_jpg10.png" />
<input type = "submit" style = "position:absolute;left:10px;top:0px;width:120px;height:40px;opacity:0;cursor:pointer;"/>
</form>
<form name = "frm" method = "get" action = "receive.html" onsubmit = "return foo()" style = "position:relative;">
<input type="hidden"  name="hid" value = "" index = "aaa">linux视频教程全套教程>电脑系统登录界面图片
<img class = "more" src = "main_jpg10.png" />
<input type = "submit" style = "position:absolute;left:10px;top:0px;width:120px;height:40px;opacity:0;cursor:pointer;"/>
</form>
<form name = "frm" method = "get" action = "receive.html" onsubmit = "return foo()" style = "position:relative;">
<input type="hidden"  name="hid" value = "" index = "bbb">
<img class = "more" src = "main_jpg10.png" />
<input type = "submit" style = "position:absolute;left:10px;top:0px;width:120px;height:40px;opacity:0;cursor:pointer;"/>
</form>
</body>
</html>
<script>
function foo(){
var frm = window.event.srcElement;
frm.hid.value = $(frm.hid).attr("index");
return true;
}
</script>
点击图⽚时,跳转⾄receive.html页⾯。页⾯的url变成:
我们想要传的字符串已经传递了过来。
然后再对当前的url进⾏字符串分割
window.location.href.split(“=”)[1]//得到lemon
我们拿到需要传来的参数之后,就可以根据这个进⾏下⼀步的处理了。
除了上述通过字符串分割来获取url传递的参数外,我们还可以通过正则匹配和window.location.search⽅法来获取。
2、通过window.location.href
譬如我们点击某个列表,需要传递⼀个字符串到detail.html页⾯,然后detail.html页⾯根据传来的值,通过ajax交互数据,加载页⾯的内容。
var index = "lemon"; var url = "receive.html?index="+index; $("#more").click(function(){ window.location.href = url; });
当前页⾯会被替换成recieve.html的页⾯,页⾯的url变为:
然后我们再⽤上⾯的⽅法提取⾃⼰需要的参数
3、通过window.location.open
如果是希望打开⼀个新页⾯,⽽不是改变当前的页⾯,那么window.location.href就不适⽤了,此时,我们需要借助于window.location.open()来实现
简单介绍有⼀下window.open()函数,window.open()有三个参数,第⼀个参数是要打开的页⾯的url,
第⼆个参数是窗⼝⽬标,第三个参数是⼀个特定字符串以及⼀个表⽰新页⾯是否取代浏览器历史集中当前加载页⾯的布尔值,通过只需要传递第⼀个参数。第⼆个参数还可以是”_blank”,”_self”,”_parent”,”_top”这样的特殊窗⼝名称,”_blank”打开新窗⼝,”_self”实现的效果同window.location.href.
继续上⾯的例⼦:
<script>
var index = "lemon";
var url = "receive.html?index="+index;
$("#more").click(function(){
window.open(url)
});
</script>
这样在点击的时候,就会打开⼀个新页⾯,页⾯的url地址与上⾯相同。
由于浏览器的安全限制,有些浏览器在弹出窗⼝配置⽅⾯增加限制,⼤多数浏览器都内置有弹出窗⼝的屏蔽程序,因此,弹出窗⼝有可能被屏蔽,在弹出窗⼝被屏蔽时,需要考虑两种可能性,⼀种是浏览器内置的屏蔽程序阻⽌弹出窗⼝,那么window.open()很可能返回Null,此时,只要监测这个返回的值就可以确定弹出窗⼝是否被屏蔽。
var newWin = window.open(url);
if(newWin == null){
alert("弹窗被阻⽌");
}
position和location的区别
另⼀种是浏览器扩展或其他程序阻⽌的弹出窗⼝,那么window.open()通常会抛出⼀个错误,因此,要像准确的检测弹出窗⼝是否被屏蔽,必须在检测返回值的同时,将window.open()封装在try-catch块中,上⾯的例⼦中可以写成如下形式:湖人直播nba
<script>
var blocked = false;
try{
var index = "lemon";
dubbo和spring cloud alibabavar url = "receive.html?index="+index;
matlab怎么画区域图$("#more").click(function(){
var newWin = window.open(url);
if(newWin == null){
blocked = true;
}
});
} catch(ex){
block = true;
}
if(blocked){
alert("弹出窗⼝被阻⽌");
}
</script>
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。