JS web localStorage传值 coolie传值


localStorage传值

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>localStorage传值页面</title>
</head>
<body>
<input type="text" value="哥哥我爱你" class="inp1" id="bbb">
<input type="button" value="添加" class="inp1 inp11" onclick="btn1()">
</body>
<script type="text/javascript">
function btn1() {
localStorage.setItem('biaoji', document.getElementsByClassName('inp1')[0].value);
};
</script>
</html>


<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>获取数据页面</title>
</head>
<body>
<div class="tianjia">
获取值
</div>
<div class="clear">
清除localStorage
</div>
<div class="zhi">

</div>
<script>
document.getElementsByClassName('clear')[0].onclick = function () {
localStorage.clear('biaoji');
}

document.getElementsByClassName('tianjia')[0].onclick = function () {
document.getElementsByClassName('zhi')[0].innerHTML = localStorage.getItem('biaoji');
}
</script>
</body>
</html>

cookie传值

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>1</title>
<script src="../jquery/jquery.2.0.0.js"></script>
<script src="./cookie.js"></script>
</head>
<body>
<div>
<span>姓名</span>
<input type="text" value="gegegegegegege" class="name">
</div>
<div>
<span>年龄</span>
<input type="text" value="24" class="age">
</div>
<span onclick="cz()">传值</span>
<script>
function cz() {
var data = {
name: $('.name').val()
, age: $('.age').val()
};
$.cookie('data', JSON.stringify(data));
}
</script>
</body>
</html>
接受数据cookie

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>2</title>
<script src="../jquery/jquery.2.0.0.js"></script>
<script src="./cookie.js"></script>
</head>
<body>
<script>
$(function () {
var jsdata = $.cookie('data');
console.log(jsdata);
});
</script>
</body>
</html>
posted @ 2019-01-14 15:19  小猴子会上树  阅读(178)  评论(0编辑  收藏  举报