javascript 为什么弹出的是 undefined?
<scripttype="text/javascript">$(function(){'usestrict';varcourse='LearnJavaScript';al...
<script type="text/javascript">
$(function () {
'use strict';
var course = 'Learn JavaScript';
alert(course); // 弹出 'Learn JavaScript'
alert(window.course); // 为什么弹出的是 undefined?
})
</script> 展开
$(function () {
'use strict';
var course = 'Learn JavaScript';
alert(course); // 弹出 'Learn JavaScript'
alert(window.course); // 为什么弹出的是 undefined?
})
</script> 展开
1个回答
2016-06-13 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
//唯一会出现undefined的情况是,你在呼叫save这个方法时没有传入value
//造成load在取值时出现undefined
下面例子
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function save(value) {
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = "savetThing" + "=" + encodeURI(value) + ";expires=" + exp.toGMTString();
}
function load() {
var cookies = document.cookie.split(";");
for( var i = 0; i < cookies.length ; i ++){
var cookieCrumbs = cookies[i].split("=");
var cookieName = cookieCrumbs[0];
var cookieValue = cookieCrumbs[1];
if( cookieName == "savetThing"){
document.getElementById("content").value=decodeURI(cookieValue);
}
}
}
function DelCookie() {
document.cookie = "savetThing"+"=;expires="+(new Date(0)).toGMTString();
reLoad();
}
function reLoad() { window.location.reload(); }
</script>
</head>
<body>
<input type="text" id="content" value="" style="width:300px;">
<input type="button" id="btn1" value="save" onclick="save('这是测试');">
<input type="button" id="btn2" value="load" onclick="load();">
<input type="button" id="btn3" value="del" onclick="DelCookie();">
</body>
</html>
//造成load在取值时出现undefined
下面例子
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function save(value) {
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = "savetThing" + "=" + encodeURI(value) + ";expires=" + exp.toGMTString();
}
function load() {
var cookies = document.cookie.split(";");
for( var i = 0; i < cookies.length ; i ++){
var cookieCrumbs = cookies[i].split("=");
var cookieName = cookieCrumbs[0];
var cookieValue = cookieCrumbs[1];
if( cookieName == "savetThing"){
document.getElementById("content").value=decodeURI(cookieValue);
}
}
}
function DelCookie() {
document.cookie = "savetThing"+"=;expires="+(new Date(0)).toGMTString();
reLoad();
}
function reLoad() { window.location.reload(); }
</script>
</head>
<body>
<input type="text" id="content" value="" style="width:300px;">
<input type="button" id="btn1" value="save" onclick="save('这是测试');">
<input type="button" id="btn2" value="load" onclick="load();">
<input type="button" id="btn3" value="del" onclick="DelCookie();">
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询