1. 通過url傳遞參數(shù)的方式
該案例是從a.html向b.html頁面?zhèn)鬟f參數(shù)
- a.html的代碼
<input type="text" value="猜猜我是誰">
<button onclick="jump()">跳轉</button>
2.點擊跳轉按鈕可以將input標簽的value值傳遞到b.html
function jump() {
var s = document.getElementsByTagName('input')[0];
location.href='7.獲取參數(shù).html?'+'txt=' + encodeURI(s.value);
}
- b.html中的代碼
<div id="box"></div>
var loc = location.href;
var n1 = loc.length;
var n2 = loc.indexOf('=');
var txt = decodeURI(loc.substr(n2+1,n1-n2));
var box = document.getElementById('box');
box.innerHTML = txt;
2. ajax獲取另一個頁面的數(shù)據(jù)
$.ajax({
url:"2.html",
type:"get",
data:"aa"
success:function (data) {
$("#a").html(data);
}
})
3. 本地存儲sessionStorage 或 localStorage (兩者用法一樣)
添加 3種方法
localStorage.infos="aaaa";
localStorage["infos"]="aaaa";
localStorage.setItem("infos","aaaa");
// console.log(localStorage.infos);
獲取
localStorage.infos;
localStorage["infos"];
localStorage.getItem("infos");
刪除 2種方法
localStorage.removeItem("infos");
localStorage.clear("infos");
4.cookie
創(chuàng)建cookie
如果要多次創(chuàng)建 cookie 嘶摊,重復使用這個方法即可。
document.cookie='key=val';
訪問cookie
document.cookie