項目中經常會出現的一種情況,有一個列表,譬如是案例列表,點擊列表中的某一項,跳轉至詳情頁面。詳情是根據所點擊的某條記錄生成的,因為案例和具體的詳情頁面,都是用戶后期自行添加的,我們開始編寫時,不可能窮盡。因此跳轉頁面時,我們需要傳遞一個參數過去,這樣我們才能通過這個參數進行數據請求,然后根據后臺返回的數據來生成頁面。因此,通過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"> <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> |