how2j.cn


工具版本兼容问题
JavaScript用于网页和用户之间的交互,比如提交的时候,进行用户名是否为空的判断。

完整的javascript由语言基础BOMDOM组成。本模块主要讲解javascript语言本身和BOM, DOM的内容请参考HTML DOM

示例 1 : 通过javascript向文档中输出文本   
示例 2 : javascript和DOM结合的一个简单例子   
示例 3 : 语言基础,BOM和DOM   

示例 1 :

通过javascript向文档中输出文本

document是javascript的内置对象,代表浏览器的文档部分
document.write("Hello Javascript"); 向文档写入字符串
"); window.frames["iframe_show1089"].document.write(decodeHtml($("textarea#stepcodeTextarea1089").val())); window.frames["iframe_show1089"].document.close(); $(window.frames["iframe_show1089"]).load(function(){ $("#iframe_show1089").height($("#iframe_show1089").contents().find("body").height()+showittryitheight); }); $("#iframe_show1089").height($("#iframe_show1089").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> document.write("Hello Javascript"); </script>
<script>
  document.write("Hello Javascript");
</script>
"); window.frames["iframe1089"].document.write(decodeHtml(code1089)); window.frames["iframe1089"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1089"]).load(function(){ $("#iframe1089").height($("#iframe1089").contents().find("body").height()+showittryitheight); }); $("#iframe1089").height($("#iframe1089").contents().find("body").height()+showittryitheight); alreadyWriteCode1089 = code1089; $("#rendering1089").hide(); $("#rendered1089").show(); } var tRereshRetry2DemoPanel1089 = setInterval(rereshRetry2DemoPanel1089,1000); var binded1089 = false; $("textarea#stepcodeTextarea1089").keyup(function(){ if(!binded1089){ $(window).bind('beforeunload',function(){ binded1089 = true; return "xxxx"; }); } var newCode = $(this).val() code1089 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1089!=newCode){ // window.frames["iframe1089"].document.write("
"); // window.frames["iframe1089"].document.write(decodeHtml($("textarea#stepcodeTextarea1089").val())); // window.frames["iframe1089"].document.close(); // $(window.frames["iframe1089"]).load(function(){ // $("#iframe1089").height($("#iframe1089").contents().find("body").height()+showittryitheight); // }); // code1089 = newCode; // } }); $(".tryButton1089").click(function(){ $("#tryDiv1089").show(); $("#stepcodeTextarea1089").focus(); $("#stepcodeTextarea1089").height(200); $("#iframe1089").height(0); window.frames["iframe1089"].document.write("
"); window.frames["iframe1089"].document.write(decodeHtml($("textarea#stepcodeTextarea1089").val())); window.frames["iframe1089"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1089"]).load(function(){ $("#iframe1089").height($("#iframe1089").contents().find("body").height()+showittryitheight); }); $("#iframe1089").height($("#iframe1089").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1089.focus(); editor1089.setSize(null, "250"); $("#rendering1089").hide(); $("#rendered1089").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 editor1089 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1089"), { 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); } } }); editor1089.on("change",function(doc){ if(!binded1089){ $(window).bind('beforeunload',function(){ binded1089 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1089 = newCode; $("textarea#stepcodeTextarea1089").val(newCode); if(alreadyWriteCode1089!=code1089){ lastModifedTime1089 = new Date().getTime(); $("#rendering1089").show(); $("#rendered1089").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1089 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1089.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1089").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

javascript和DOM结合的一个简单例子

这是一个javascript和DOM结合的一个简单例子

onclick="..." 表示点击button之后做的事情
document.getElementById 根据id获取指定的元素
.style.display='none' 隐藏
.style.display='block' 显示(准确的讲,以block的形式显示)

onclick,getElementById,style.display 这些内容,是HTML DOM 应该到才会用到的知识和概念
"); window.frames["iframe_show2094"].document.write(decodeHtml($("textarea#stepcodeTextarea2094").val())); window.frames["iframe_show2094"].document.close(); $(window.frames["iframe_show2094"]).load(function(){ $("#iframe_show2094").height($("#iframe_show2094").contents().find("body").height()+showittryitheight); }); $("#iframe_show2094").height($("#iframe_show2094").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<button onclick="document.getElementById('text').style.display='none'">隐藏文本</button> <button onclick="document.getElementById('text').style.display='block'">显示文本</button> <p id="text"> 这个是一段可以被javascript隐藏的文本</p>
<button onclick="document.getElementById('text').style.display='none'">隐藏文本</button>

<button onclick="document.getElementById('text').style.display='block'">显示文本</button>

<p id="text"> 这个是一段可以被javascript隐藏的文本</p>
"); window.frames["iframe2094"].document.write(decodeHtml(code2094)); window.frames["iframe2094"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe2094"]).load(function(){ $("#iframe2094").height($("#iframe2094").contents().find("body").height()+showittryitheight); }); $("#iframe2094").height($("#iframe2094").contents().find("body").height()+showittryitheight); alreadyWriteCode2094 = code2094; $("#rendering2094").hide(); $("#rendered2094").show(); } var tRereshRetry2DemoPanel2094 = setInterval(rereshRetry2DemoPanel2094,1000); var binded2094 = false; $("textarea#stepcodeTextarea2094").keyup(function(){ if(!binded2094){ $(window).bind('beforeunload',function(){ binded2094 = true; return "xxxx"; }); } var newCode = $(this).val() code2094 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code2094!=newCode){ // window.frames["iframe2094"].document.write("
"); // window.frames["iframe2094"].document.write(decodeHtml($("textarea#stepcodeTextarea2094").val())); // window.frames["iframe2094"].document.close(); // $(window.frames["iframe2094"]).load(function(){ // $("#iframe2094").height($("#iframe2094").contents().find("body").height()+showittryitheight); // }); // code2094 = newCode; // } }); $(".tryButton2094").click(function(){ $("#tryDiv2094").show(); $("#stepcodeTextarea2094").focus(); $("#stepcodeTextarea2094").height(200); $("#iframe2094").height(0); window.frames["iframe2094"].document.write("
"); window.frames["iframe2094"].document.write(decodeHtml($("textarea#stepcodeTextarea2094").val())); window.frames["iframe2094"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe2094"]).load(function(){ $("#iframe2094").height($("#iframe2094").contents().find("body").height()+showittryitheight); }); $("#iframe2094").height($("#iframe2094").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor2094.focus(); editor2094.setSize(null, "250"); $("#rendering2094").hide(); $("#rendered2094").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 editor2094 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea2094"), { 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); } } }); editor2094.on("change",function(doc){ if(!binded2094){ $(window).bind('beforeunload',function(){ binded2094 = true; return "xxxx"; }); } var newCode = doc.getValue(); code2094 = newCode; $("textarea#stepcodeTextarea2094").val(newCode); if(alreadyWriteCode2094!=code2094){ lastModifedTime2094 = new Date().getTime(); $("#rendering2094").show(); $("#rendered2094").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor2094 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor2094.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv2094").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

语言基础,BOM和DOM

完整的javascript由语言基础BOMDOM组成。

只有通过javascript操作DOM对象的时候,才会带来很好的实用效果,而本章节只会涉及到语言基础 和 BOM部分,DOM部分在下个章节展开
语言基础,BOM和DOM


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


关于 前端部分-JavaScript-Hello JavaScript 的提问

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

上传截图