how2j.cn


工具版本兼容问题
使用var声明一个变量

示例 1 : 使用var声明一个变量   
示例 2 : 不使用var   
示例 3 : 变量命名   

示例 1 :

使用var声明一个变量

"); window.frames["iframe_show1072"].document.write(decodeHtml($("textarea#stepcodeTextarea1072").val())); window.frames["iframe_show1072"].document.close(); $(window.frames["iframe_show1072"]).load(function(){ $("#iframe_show1072").height($("#iframe_show1072").contents().find("body").height()+showittryitheight); }); $("#iframe_show1072").height($("#iframe_show1072").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var x = 10; document.write("变量x的值:"+x); </script>
<script>
  var x = 10;
  document.write("变量x的值:"+x);
</script>
"); window.frames["iframe1072"].document.write(decodeHtml(code1072)); window.frames["iframe1072"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1072"]).load(function(){ $("#iframe1072").height($("#iframe1072").contents().find("body").height()+showittryitheight); }); $("#iframe1072").height($("#iframe1072").contents().find("body").height()+showittryitheight); alreadyWriteCode1072 = code1072; $("#rendering1072").hide(); $("#rendered1072").show(); } var tRereshRetry2DemoPanel1072 = setInterval(rereshRetry2DemoPanel1072,1000); var binded1072 = false; $("textarea#stepcodeTextarea1072").keyup(function(){ if(!binded1072){ $(window).bind('beforeunload',function(){ binded1072 = true; return "xxxx"; }); } var newCode = $(this).val() code1072 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1072!=newCode){ // window.frames["iframe1072"].document.write("
"); // window.frames["iframe1072"].document.write(decodeHtml($("textarea#stepcodeTextarea1072").val())); // window.frames["iframe1072"].document.close(); // $(window.frames["iframe1072"]).load(function(){ // $("#iframe1072").height($("#iframe1072").contents().find("body").height()+showittryitheight); // }); // code1072 = newCode; // } }); $(".tryButton1072").click(function(){ $("#tryDiv1072").show(); $("#stepcodeTextarea1072").focus(); $("#stepcodeTextarea1072").height(200); $("#iframe1072").height(0); window.frames["iframe1072"].document.write("
"); window.frames["iframe1072"].document.write(decodeHtml($("textarea#stepcodeTextarea1072").val())); window.frames["iframe1072"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1072"]).load(function(){ $("#iframe1072").height($("#iframe1072").contents().find("body").height()+showittryitheight); }); $("#iframe1072").height($("#iframe1072").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1072.focus(); editor1072.setSize(null, "250"); $("#rendering1072").hide(); $("#rendered1072").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1072 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1072"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1072.on("change",function(doc){ if(!binded1072){ $(window).bind('beforeunload',function(){ binded1072 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1072 = newCode; $("textarea#stepcodeTextarea1072").val(newCode); if(alreadyWriteCode1072!=code1072){ lastModifedTime1072 = new Date().getTime(); $("#rendering1072").show(); $("#rendered1072").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1072 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1072.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1072").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

不使用var

关键字var 可有可无,本实例演示不使用var声明的变量
"); window.frames["iframe_show1073"].document.write(decodeHtml($("textarea#stepcodeTextarea1073").val())); window.frames["iframe_show1073"].document.close(); $(window.frames["iframe_show1073"]).load(function(){ $("#iframe_show1073").height($("#iframe_show1073").contents().find("body").height()+showittryitheight); }); $("#iframe_show1073").height($("#iframe_show1073").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> x = 10; document.write("没有用var声明的变量x的值:"+x); </script>
<script>
  x = 10;
  document.write("没有用var声明的变量x的值:"+x);
</script>
"); window.frames["iframe1073"].document.write(decodeHtml(code1073)); window.frames["iframe1073"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1073"]).load(function(){ $("#iframe1073").height($("#iframe1073").contents().find("body").height()+showittryitheight); }); $("#iframe1073").height($("#iframe1073").contents().find("body").height()+showittryitheight); alreadyWriteCode1073 = code1073; $("#rendering1073").hide(); $("#rendered1073").show(); } var tRereshRetry2DemoPanel1073 = setInterval(rereshRetry2DemoPanel1073,1000); var binded1073 = false; $("textarea#stepcodeTextarea1073").keyup(function(){ if(!binded1073){ $(window).bind('beforeunload',function(){ binded1073 = true; return "xxxx"; }); } var newCode = $(this).val() code1073 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1073!=newCode){ // window.frames["iframe1073"].document.write("
"); // window.frames["iframe1073"].document.write(decodeHtml($("textarea#stepcodeTextarea1073").val())); // window.frames["iframe1073"].document.close(); // $(window.frames["iframe1073"]).load(function(){ // $("#iframe1073").height($("#iframe1073").contents().find("body").height()+showittryitheight); // }); // code1073 = newCode; // } }); $(".tryButton1073").click(function(){ $("#tryDiv1073").show(); $("#stepcodeTextarea1073").focus(); $("#stepcodeTextarea1073").height(200); $("#iframe1073").height(0); window.frames["iframe1073"].document.write("
"); window.frames["iframe1073"].document.write(decodeHtml($("textarea#stepcodeTextarea1073").val())); window.frames["iframe1073"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1073"]).load(function(){ $("#iframe1073").height($("#iframe1073").contents().find("body").height()+showittryitheight); }); $("#iframe1073").height($("#iframe1073").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1073.focus(); editor1073.setSize(null, "250"); $("#rendering1073").hide(); $("#rendered1073").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1073 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1073"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1073.on("change",function(doc){ if(!binded1073){ $(window).bind('beforeunload',function(){ binded1073 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1073 = newCode; $("textarea#stepcodeTextarea1073").val(newCode); if(alreadyWriteCode1073!=code1073){ lastModifedTime1073 = new Date().getTime(); $("#rendering1073").show(); $("#rendered1073").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1073 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1073.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1073").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

变量命名

命名规则和java差不多
可以使用
开头可以用 _$和字母
其他部分可以用 $ _ 字母或者数字
这些是合法的:

var $a;
var _b;
var ab123;


这些是不合法的:

var 3$a;
var a%;
var b*;
var (6@


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JavaScript-变量 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图