how2j.cn


工具版本兼容问题
一个元素节点的style属性即对应的css

步骤 1 : 隐藏和显示   
步骤 2 : 改变背景色   
步骤 3 : 练习-表格斑马线   
步骤 4 : 答案-表格斑马线   

步骤 1 :

隐藏和显示

通过给元素的style.display 赋值,改变显示还是隐藏
"); window.frames["iframe_show2934"].document.write(decodeHtml($("textarea#stepcodeTextarea2934").val())); window.frames["iframe_show2934"].document.close(); $(window.frames["iframe_show2934"]).load(function(){ $("#iframe_show2934").height($("#iframe_show2934").contents().find("body").height()+showittryitheight); }); $("#iframe_show2934").height($("#iframe_show2934").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<button onclick="hide()">隐藏div</button> <button onclick="show()">显示div</button> <br> <br> <div id="d"> 这是一个div </div> <script> function hide(){ var d = document.getElementById("d"); d.style.display="none"; } function show(){ var d = document.getElementById("d"); d.style.display="block"; } </script>
"); window.frames["iframe2934"].document.write(decodeHtml(code2934)); window.frames["iframe2934"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe2934"]).load(function(){ $("#iframe2934").height($("#iframe2934").contents().find("body").height()+showittryitheight); }); $("#iframe2934").height($("#iframe2934").contents().find("body").height()+showittryitheight); alreadyWriteCode2934 = code2934; $("#rendering2934").hide(); $("#rendered2934").show(); } var tRereshRetry2DemoPanel2934 = setInterval(rereshRetry2DemoPanel2934,1000); var binded2934 = false; $("textarea#stepcodeTextarea2934").keyup(function(){ if(!binded2934){ $(window).bind('beforeunload',function(){ binded2934 = true; return "xxxx"; }); } var newCode = $(this).val() code2934 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code2934!=newCode){ // window.frames["iframe2934"].document.write("
"); // window.frames["iframe2934"].document.write(decodeHtml($("textarea#stepcodeTextarea2934").val())); // window.frames["iframe2934"].document.close(); // $(window.frames["iframe2934"]).load(function(){ // $("#iframe2934").height($("#iframe2934").contents().find("body").height()+showittryitheight); // }); // code2934 = newCode; // } }); $(".tryButton2934").click(function(){ $("#tryDiv2934").show(); $("#stepcodeTextarea2934").focus(); $("#stepcodeTextarea2934").height(200); $("#iframe2934").height(0); window.frames["iframe2934"].document.write("
"); window.frames["iframe2934"].document.write(decodeHtml($("textarea#stepcodeTextarea2934").val())); window.frames["iframe2934"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe2934"]).load(function(){ $("#iframe2934").height($("#iframe2934").contents().find("body").height()+showittryitheight); }); $("#iframe2934").height($("#iframe2934").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor2934.focus(); editor2934.setSize(null, "250"); $("#rendering2934").hide(); $("#rendered2934").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 editor2934 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea2934"), { 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); } } }); editor2934.on("change",function(doc){ if(!binded2934){ $(window).bind('beforeunload',function(){ binded2934 = true; return "xxxx"; }); } var newCode = doc.getValue(); code2934 = newCode; $("textarea#stepcodeTextarea2934").val(newCode); if(alreadyWriteCode2934!=code2934){ lastModifedTime2934 = new Date().getTime(); $("#rendering2934").show(); $("#rendered2934").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor2934 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor2934.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv2934").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

改变背景色

通过给元素的style.backgroundColor 赋值,修改样式
你也许注意到了style.backgroundColor 这里的backgroundColorcss中的背景色background-color 是有所不同的
换句话说,通过DOM的style去修改样式,需要重新记忆另一套样式名称,这是很累赘的事情。
最好能够通过CSS的属性名,直接就进行修改了。比如通过这样的方式:

d1.css("background-color","green");

这样就仅仅需要使用CSS原本的属性名即可了。
Javascript并不提供这样的解决方案,但是到了JQuery就提供了这样的解决方案,请参考 通过JQuery设置CSS
"); window.frames["iframe_show1198"].document.write(decodeHtml($("textarea#stepcodeTextarea1198").val())); window.frames["iframe_show1198"].document.close(); $(window.frames["iframe_show1198"]).load(function(){ $("#iframe_show1198").height($("#iframe_show1198").contents().find("body").height()+showittryitheight); }); $("#iframe_show1198").height($("#iframe_show1198").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<div id="d1" style="background-color:pink">Hello HTML DOM</div> <button onclick="change()">改变div的背景色</button> <script> function change(){ var d1 = document.getElementById("d1"); d1.style.backgroundColor="green"; } </script>
<div id="d1" style="background-color:pink">Hello HTML DOM</div>

<button onclick="change()">改变div的背景色</button>

<script>
function change(){
  var d1 = document.getElementById("d1");
  d1.style.backgroundColor="green";
}
</script>

"); window.frames["iframe1198"].document.write(decodeHtml(code1198)); window.frames["iframe1198"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1198"]).load(function(){ $("#iframe1198").height($("#iframe1198").contents().find("body").height()+showittryitheight); }); $("#iframe1198").height($("#iframe1198").contents().find("body").height()+showittryitheight); alreadyWriteCode1198 = code1198; $("#rendering1198").hide(); $("#rendered1198").show(); } var tRereshRetry2DemoPanel1198 = setInterval(rereshRetry2DemoPanel1198,1000); var binded1198 = false; $("textarea#stepcodeTextarea1198").keyup(function(){ if(!binded1198){ $(window).bind('beforeunload',function(){ binded1198 = true; return "xxxx"; }); } var newCode = $(this).val() code1198 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1198!=newCode){ // window.frames["iframe1198"].document.write("
"); // window.frames["iframe1198"].document.write(decodeHtml($("textarea#stepcodeTextarea1198").val())); // window.frames["iframe1198"].document.close(); // $(window.frames["iframe1198"]).load(function(){ // $("#iframe1198").height($("#iframe1198").contents().find("body").height()+showittryitheight); // }); // code1198 = newCode; // } }); $(".tryButton1198").click(function(){ $("#tryDiv1198").show(); $("#stepcodeTextarea1198").focus(); $("#stepcodeTextarea1198").height(200); $("#iframe1198").height(0); window.frames["iframe1198"].document.write("
"); window.frames["iframe1198"].document.write(decodeHtml($("textarea#stepcodeTextarea1198").val())); window.frames["iframe1198"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1198"]).load(function(){ $("#iframe1198").height($("#iframe1198").contents().find("body").height()+showittryitheight); }); $("#iframe1198").height($("#iframe1198").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1198.focus(); editor1198.setSize(null, "250"); $("#rendering1198").hide(); $("#rendered1198").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 editor1198 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1198"), { 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); } } }); editor1198.on("change",function(doc){ if(!binded1198){ $(window).bind('beforeunload',function(){ binded1198 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1198 = newCode; $("textarea#stepcodeTextarea1198").val(newCode); if(alreadyWriteCode1198!=code1198){ lastModifedTime1198 = new Date().getTime(); $("#rendering1198").show(); $("#rendered1198").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1198 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1198.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1198").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

练习-表格斑马线

练习难度
Or  姿势不对,事倍功半! 点击查看做练习的正确姿势
对CSS中的练习-表格斑马线,使用javascript来实现
"); window.frames["iframe_show2937"].document.write(decodeHtml($("textarea#stepcodeTextarea2937").val())); window.frames["iframe_show2937"].document.close(); $(window.frames["iframe_show2937"]).load(function(){ $("#iframe_show2937").height($("#iframe_show2937").contents().find("body").height()+showittryitheight); }); $("#iframe_show2937").height($("#iframe_show2937").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
<style> table{ border-collapse:collapse; width:90%; } tr{ border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: lightgray; height:35px; } td{ width:25%; text-align:center; } </style> <table id="t"> <tr > <td>id</td> <td>名称</td> <td>血量</td> <td>伤害</td> </tr> <tr > <td>1</td> <td>gareen</td> <td>340</td> <td>58</td> </tr> <tr > <td>2</td> <td>teemo</td> <td>320</td> <td>76</td> </tr> <tr > <td>3</td> <td>annie</td> <td>380</td> <td>38</td> </tr> <tr > <td>4</td> <td>deadbrother</td> <td>400</td> <td>90</td> </tr> </table> <script> var trs = document.getElementsByTagName("tr"); for(i=0;i<trs.length;i++){ if(1==i%2) trs[i].style.backgroundColor='#f8f8f8'; } </script>
"); window.frames["iframe2937"].document.write(decodeHtml(code2937)); window.frames["iframe2937"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe2937"]).load(function(){ $("#iframe2937").height($("#iframe2937").contents().find("body").height()+showittryitheight); }); $("#iframe2937").height($("#iframe2937").contents().find("body").height()+showittryitheight); alreadyWriteCode2937 = code2937; $("#rendering2937").hide(); $("#rendered2937").show(); } var tRereshRetry2DemoPanel2937 = setInterval(rereshRetry2DemoPanel2937,1000); var binded2937 = false; $("textarea#stepcodeTextarea2937").keyup(function(){ if(!binded2937){ $(window).bind('beforeunload',function(){ binded2937 = true; return "xxxx"; }); } var newCode = $(this).val() code2937 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code2937!=newCode){ // window.frames["iframe2937"].document.write("
"); // window.frames["iframe2937"].document.write(decodeHtml($("textarea#stepcodeTextarea2937").val())); // window.frames["iframe2937"].document.close(); // $(window.frames["iframe2937"]).load(function(){ // $("#iframe2937").height($("#iframe2937").contents().find("body").height()+showittryitheight); // }); // code2937 = newCode; // } }); $(".tryButton2937").click(function(){ $("#tryDiv2937").show(); $("#stepcodeTextarea2937").focus(); $("#stepcodeTextarea2937").height(200); $("#iframe2937").height(0); window.frames["iframe2937"].document.write("
"); window.frames["iframe2937"].document.write(decodeHtml($("textarea#stepcodeTextarea2937").val())); window.frames["iframe2937"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe2937"]).load(function(){ $("#iframe2937").height($("#iframe2937").contents().find("body").height()+showittryitheight); }); $("#iframe2937").height($("#iframe2937").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor2937.focus(); editor2937.setSize(null, "250"); $("#rendering2937").hide(); $("#rendered2937").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 editor2937 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea2937"), { 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); } } }); editor2937.on("change",function(doc){ if(!binded2937){ $(window).bind('beforeunload',function(){ binded2937 = true; return "xxxx"; }); } var newCode = doc.getValue(); code2937 = newCode; $("textarea#stepcodeTextarea2937").val(newCode); if(alreadyWriteCode2937!=code2937){ lastModifedTime2937 = new Date().getTime(); $("#rendering2937").show(); $("#rendered2937").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor2937 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor2937.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv2937").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

答案-表格斑马线

在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多


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


关于 前端部分-HTML DOM-样式 的提问

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

上传截图