how2j.cn


工具版本兼容问题
选择器主要分3种
元素选择器
id选择器
类选择器

示例 1 : 元素选择器   
示例 2 : id选择器   
示例 3 : 类选择器   
示例 4 : 更准确的选择   

示例 1 :

元素选择器

元素选择器通过标签名选择元素
在实例中,所有的p都被设置成红色
"); window.frames["iframe_show449"].document.write(decodeHtml($("textarea#stepcodeTextarea449").val())); window.frames["iframe_show449"].document.close(); $(window.frames["iframe_show449"]).load(function(){ $("#iframe_show449").height($("#iframe_show449").contents().find("body").height()+showittryitheight); }); $("#iframe_show449").height($("#iframe_show449").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<style> p{ color:red; } </style> <p>p元素</p> <p>p元素</p> <p>p元素</p>
<style>
p{
  color:red;
}
</style>

<p>p元素</p>
<p>p元素</p>
<p>p元素</p>

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


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

id选择器

通过id选择元素
注: 一个元素的id应该是唯一的。另一个元素不应该重复使用
"); window.frames["iframe_show450"].document.write(decodeHtml($("textarea#stepcodeTextarea450").val())); window.frames["iframe_show450"].document.close(); $(window.frames["iframe_show450"]).load(function(){ $("#iframe_show450").height($("#iframe_show450").contents().find("body").height()+showittryitheight); }); $("#iframe_show450").height($("#iframe_show450").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<style> p{ color:red; } #p1{ color:blue; } #p2{ color:green; } </style> <p>没有id的p</p> <p id="p1">id=p1的p</p> <p id="p2">id=p2的p</p>
<style>
p{
  color:red;
}
#p1{
  color:blue;
}
#p2{
  color:green;
}
</style>

<p>没有id的p</p>
<p id="p1">id=p1的p</p>
<p id="p2">id=p2的p</p>
"); window.frames["iframe450"].document.write(decodeHtml(code450)); window.frames["iframe450"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe450"]).load(function(){ $("#iframe450").height($("#iframe450").contents().find("body").height()+showittryitheight); }); $("#iframe450").height($("#iframe450").contents().find("body").height()+showittryitheight); alreadyWriteCode450 = code450; $("#rendering450").hide(); $("#rendered450").show(); } var tRereshRetry2DemoPanel450 = setInterval(rereshRetry2DemoPanel450,1000); var binded450 = false; $("textarea#stepcodeTextarea450").keyup(function(){ if(!binded450){ $(window).bind('beforeunload',function(){ binded450 = true; return "xxxx"; }); } var newCode = $(this).val() code450 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code450!=newCode){ // window.frames["iframe450"].document.write("
"); // window.frames["iframe450"].document.write(decodeHtml($("textarea#stepcodeTextarea450").val())); // window.frames["iframe450"].document.close(); // $(window.frames["iframe450"]).load(function(){ // $("#iframe450").height($("#iframe450").contents().find("body").height()+showittryitheight); // }); // code450 = newCode; // } }); $(".tryButton450").click(function(){ $("#tryDiv450").show(); $("#stepcodeTextarea450").focus(); $("#stepcodeTextarea450").height(200); $("#iframe450").height(0); window.frames["iframe450"].document.write("
"); window.frames["iframe450"].document.write(decodeHtml($("textarea#stepcodeTextarea450").val())); window.frames["iframe450"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe450"]).load(function(){ $("#iframe450").height($("#iframe450").contents().find("body").height()+showittryitheight); }); $("#iframe450").height($("#iframe450").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor450.focus(); editor450.setSize(null, "250"); $("#rendering450").hide(); $("#rendered450").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 editor450 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea450"), { 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); } } }); editor450.on("change",function(doc){ if(!binded450){ $(window).bind('beforeunload',function(){ binded450 = true; return "xxxx"; }); } var newCode = doc.getValue(); code450 = newCode; $("textarea#stepcodeTextarea450").val(newCode); if(alreadyWriteCode450!=code450){ lastModifedTime450 = new Date().getTime(); $("#rendering450").show(); $("#rendered450").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor450 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor450.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv450").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

类选择器

当需要多个元素,都使用同样的css的时候,就会使用类选择器
"); window.frames["iframe_show451"].document.write(decodeHtml($("textarea#stepcodeTextarea451").val())); window.frames["iframe_show451"].document.close(); $(window.frames["iframe_show451"]).load(function(){ $("#iframe_show451").height($("#iframe_show451").contents().find("body").height()+showittryitheight); }); $("#iframe_show451").height($("#iframe_show451").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<style> .pre{ color:blue; } .after{ color:green; } </style> <p class="pre">前3个</p> <p class="pre">前3个</p> <p class="pre">前3个</p> <p class="after">后3个</p> <p class="after">后3个</p> <p class="after">后3个</p>
<style>
.pre{
  color:blue;
}
.after{
  color:green;
}
</style>

<p class="pre">前3个</p>
<p class="pre">前3个</p>
<p class="pre">前3个</p>

<p class="after">后3个</p>
<p class="after">后3个</p>
<p class="after">后3个</p>

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


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

更准确的选择

同时根据元素名和class来选择
p.blue
"); window.frames["iframe_show466"].document.write(decodeHtml($("textarea#stepcodeTextarea466").val())); window.frames["iframe_show466"].document.close(); $(window.frames["iframe_show466"]).load(function(){ $("#iframe_show466").height($("#iframe_show466").contents().find("body").height()+showittryitheight); }); $("#iframe_show466").height($("#iframe_show466").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<style> p.blue{ color:blue; } </style> <p class="blue">class=blue的p</p> <span class="blue">class=blue的span</span>
<style>

p.blue{
  color:blue;
}

</style>

<p class="blue">class=blue的p</p>

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


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


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


关于 前端部分-CSS-选择器 的提问

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

上传截图