how2j.cn

关键字 简介 示例代码
attr
获取
示例代码
attr(属性,值)
修改
示例代码
removeAttr
删除
示例代码
prop与attr
prop与attr的区别
示例代码
示例 1 : 获取   
示例 2 : 修改   
示例 3 : 删除   
示例 4 : prop与attr的区别   

示例 1 :

获取

通过attr()获取一个元素的属性
"); window.frames["iframe_show980"].document.write(decodeHtml($("textarea#stepcodeTextarea980").val())); window.frames["iframe_show980"].document.close(); $(window.frames["iframe_show980"]).load(function(){ $("#iframe_show980").height($("#iframe_show980").contents().find("body").height()+showittryitheight); }); $("#iframe_show980").height($("#iframe_show980").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> $(function(){ $("#b1").click(function(){ alert("align属性是:" + $("#h").attr("align") ); }); $("#b2").click(function(){ alert("game属性是:" + $("#h").attr("game") ); }); }); </script> <button id="b1">获取align属性</button> <button id="b2">获取自定义属性 game</button> <br> <br> <h1 id="h" align="center" game="LOL">居中标题</h1>
"); window.frames["iframe980"].document.write(decodeHtml(code980)); window.frames["iframe980"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe980"]).load(function(){ $("#iframe980").height($("#iframe980").contents().find("body").height()+showittryitheight); }); $("#iframe980").height($("#iframe980").contents().find("body").height()+showittryitheight); alreadyWriteCode980 = code980; $("#rendering980").hide(); $("#rendered980").show(); } var tRereshRetry2DemoPanel980 = setInterval(rereshRetry2DemoPanel980,1000); var binded980 = false; $("textarea#stepcodeTextarea980").keyup(function(){ if(!binded980){ $(window).bind('beforeunload',function(){ binded980 = true; return "xxxx"; }); } var newCode = $(this).val() code980 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code980!=newCode){ // window.frames["iframe980"].document.write("
"); // window.frames["iframe980"].document.write(decodeHtml($("textarea#stepcodeTextarea980").val())); // window.frames["iframe980"].document.close(); // $(window.frames["iframe980"]).load(function(){ // $("#iframe980").height($("#iframe980").contents().find("body").height()+showittryitheight); // }); // code980 = newCode; // } }); $(".tryButton980").click(function(){ $("#tryDiv980").show(); $("#stepcodeTextarea980").focus(); $("#stepcodeTextarea980").height(200); $("#iframe980").height(0); window.frames["iframe980"].document.write("
"); window.frames["iframe980"].document.write(decodeHtml($("textarea#stepcodeTextarea980").val())); window.frames["iframe980"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe980"]).load(function(){ $("#iframe980").height($("#iframe980").contents().find("body").height()+showittryitheight); }); $("#iframe980").height($("#iframe980").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor980.focus(); editor980.setSize(null, "250"); $("#rendering980").hide(); $("#rendered980").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 editor980 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea980"), { 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); } } }); editor980.on("change",function(doc){ if(!binded980){ $(window).bind('beforeunload',function(){ binded980 = true; return "xxxx"; }); } var newCode = doc.getValue(); code980 = newCode; $("textarea#stepcodeTextarea980").val(newCode); if(alreadyWriteCode980!=code980){ lastModifedTime980 = new Date().getTime(); $("#rendering980").show(); $("#rendered980").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor980 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor980.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv980").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

修改

通过attr(attr,value)修改属性
"); window.frames["iframe_show981"].document.write(decodeHtml($("textarea#stepcodeTextarea981").val())); window.frames["iframe_show981"].document.close(); $(window.frames["iframe_show981"]).load(function(){ $("#iframe_show981").height($("#iframe_show981").contents().find("body").height()+showittryitheight); }); $("#iframe_show981").height($("#iframe_show981").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> $(function(){ $("#b1").click(function(){ $("#h").attr("align","right") ; }); }); </script> <button id="b1">修改align属性为right</button> <br> <br> <h1 id="h" align="center" >居中标题</h1>
<script src="http://127.0.0.1/study/jquery.min.js"></script>
  
<script>
$(function(){
   $("#b1").click(function(){
      $("#h").attr("align","right") ;
   });

});
  
</script>
  
<button id="b1">修改align属性为right</button>
  
<br>
<br>
  
<h1 id="h" align="center" >居中标题</h1>
"); window.frames["iframe981"].document.write(decodeHtml(code981)); window.frames["iframe981"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe981"]).load(function(){ $("#iframe981").height($("#iframe981").contents().find("body").height()+showittryitheight); }); $("#iframe981").height($("#iframe981").contents().find("body").height()+showittryitheight); alreadyWriteCode981 = code981; $("#rendering981").hide(); $("#rendered981").show(); } var tRereshRetry2DemoPanel981 = setInterval(rereshRetry2DemoPanel981,1000); var binded981 = false; $("textarea#stepcodeTextarea981").keyup(function(){ if(!binded981){ $(window).bind('beforeunload',function(){ binded981 = true; return "xxxx"; }); } var newCode = $(this).val() code981 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code981!=newCode){ // window.frames["iframe981"].document.write("
"); // window.frames["iframe981"].document.write(decodeHtml($("textarea#stepcodeTextarea981").val())); // window.frames["iframe981"].document.close(); // $(window.frames["iframe981"]).load(function(){ // $("#iframe981").height($("#iframe981").contents().find("body").height()+showittryitheight); // }); // code981 = newCode; // } }); $(".tryButton981").click(function(){ $("#tryDiv981").show(); $("#stepcodeTextarea981").focus(); $("#stepcodeTextarea981").height(200); $("#iframe981").height(0); window.frames["iframe981"].document.write("
"); window.frames["iframe981"].document.write(decodeHtml($("textarea#stepcodeTextarea981").val())); window.frames["iframe981"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe981"]).load(function(){ $("#iframe981").height($("#iframe981").contents().find("body").height()+showittryitheight); }); $("#iframe981").height($("#iframe981").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor981.focus(); editor981.setSize(null, "250"); $("#rendering981").hide(); $("#rendered981").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 editor981 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea981"), { 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); } } }); editor981.on("change",function(doc){ if(!binded981){ $(window).bind('beforeunload',function(){ binded981 = true; return "xxxx"; }); } var newCode = doc.getValue(); code981 = newCode; $("textarea#stepcodeTextarea981").val(newCode); if(alreadyWriteCode981!=code981){ lastModifedTime981 = new Date().getTime(); $("#rendering981").show(); $("#rendered981").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor981 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor981.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv981").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

删除

通过removeAttr(attr)删除属性
"); window.frames["iframe_show982"].document.write(decodeHtml($("textarea#stepcodeTextarea982").val())); window.frames["iframe_show982"].document.close(); $(window.frames["iframe_show982"]).load(function(){ $("#iframe_show982").height($("#iframe_show982").contents().find("body").height()+showittryitheight); }); $("#iframe_show982").height($("#iframe_show982").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> $(function(){ $("#b1").click(function(){ $("#h").removeAttr("align"); }); }); </script> <button id="b1">删除align属性</button> <br> <br> <h1 id="h" align="center" game="LOL">居中标题</h1>
<script src="http://127.0.0.1/study/jquery.min.js"></script>
  
<script>
$(function(){
   $("#b1").click(function(){
         $("#h").removeAttr("align");
   });
});
  
</script>
  
<button id="b1">删除align属性</button>

<br>
<br>
  
<h1 id="h" align="center" game="LOL">居中标题</h1>
"); window.frames["iframe982"].document.write(decodeHtml(code982)); window.frames["iframe982"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe982"]).load(function(){ $("#iframe982").height($("#iframe982").contents().find("body").height()+showittryitheight); }); $("#iframe982").height($("#iframe982").contents().find("body").height()+showittryitheight); alreadyWriteCode982 = code982; $("#rendering982").hide(); $("#rendered982").show(); } var tRereshRetry2DemoPanel982 = setInterval(rereshRetry2DemoPanel982,1000); var binded982 = false; $("textarea#stepcodeTextarea982").keyup(function(){ if(!binded982){ $(window).bind('beforeunload',function(){ binded982 = true; return "xxxx"; }); } var newCode = $(this).val() code982 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code982!=newCode){ // window.frames["iframe982"].document.write("
"); // window.frames["iframe982"].document.write(decodeHtml($("textarea#stepcodeTextarea982").val())); // window.frames["iframe982"].document.close(); // $(window.frames["iframe982"]).load(function(){ // $("#iframe982").height($("#iframe982").contents().find("body").height()+showittryitheight); // }); // code982 = newCode; // } }); $(".tryButton982").click(function(){ $("#tryDiv982").show(); $("#stepcodeTextarea982").focus(); $("#stepcodeTextarea982").height(200); $("#iframe982").height(0); window.frames["iframe982"].document.write("
"); window.frames["iframe982"].document.write(decodeHtml($("textarea#stepcodeTextarea982").val())); window.frames["iframe982"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe982"]).load(function(){ $("#iframe982").height($("#iframe982").contents().find("body").height()+showittryitheight); }); $("#iframe982").height($("#iframe982").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor982.focus(); editor982.setSize(null, "250"); $("#rendering982").hide(); $("#rendered982").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 editor982 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea982"), { 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); } } }); editor982.on("change",function(doc){ if(!binded982){ $(window).bind('beforeunload',function(){ binded982 = true; return "xxxx"; }); } var newCode = doc.getValue(); code982 = newCode; $("textarea#stepcodeTextarea982").val(newCode); if(alreadyWriteCode982!=code982){ lastModifedTime982 = new Date().getTime(); $("#rendering982").show(); $("#rendered982").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor982 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor982.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv982").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

prop与attr的区别

prop一样attr也可以用来获取与设置元素的属性。
区别在于,对于自定义属性选中属性的处理。
选中属性指的是 checked,selected 这2种属性
1. 对于自定义属性 attr能够获取,prop不能获取
2. 对于选中属性
attr 只能获取初始值, 无论是否变化
prop 能够访问变化后的值,并且以true|false的布尔型返回。
所以在访问表单对象属性的时候,应该采用prop而非attr
"); window.frames["iframe_show1223"].document.write(decodeHtml($("textarea#stepcodeTextarea1223").val())); window.frames["iframe_show1223"].document.close(); $(window.frames["iframe_show1223"]).load(function(){ $("#iframe_show1223").height($("#iframe_show1223").contents().find("body").height()+showittryitheight); }); $("#iframe_show1223").height($("#iframe_show1223").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> $(function(){ $("#b1").click(function(){ alert("game属性是:" + $("#c").attr("game") ); }); $("#b2").click(function(){ alert("game属性是:" + $("#c").prop("game") ); }); $("#b3").click(function(){ alert("checked属性是:" + $("#c").attr("checked") ); }); $("#b4").click(function(){ alert("checked属性是:" + $("#c").prop("checked") ); }); }); </script> <style> button{ display:block; } </style> <button id="b1">通过attr获取自定义属性 game</button> <button id="b2">通过prop获取自定义属性 game</button> <button id="b3">通过attr获取 checked属性</button> <button id="b4">通过prop获取 checked属性</button> <br> <br> <input type="checkbox" id="c" game="LOL" checked="checked"> 选中的复选框
"); window.frames["iframe1223"].document.write(decodeHtml(code1223)); window.frames["iframe1223"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1223"]).load(function(){ $("#iframe1223").height($("#iframe1223").contents().find("body").height()+showittryitheight); }); $("#iframe1223").height($("#iframe1223").contents().find("body").height()+showittryitheight); alreadyWriteCode1223 = code1223; $("#rendering1223").hide(); $("#rendered1223").show(); } var tRereshRetry2DemoPanel1223 = setInterval(rereshRetry2DemoPanel1223,1000); var binded1223 = false; $("textarea#stepcodeTextarea1223").keyup(function(){ if(!binded1223){ $(window).bind('beforeunload',function(){ binded1223 = true; return "xxxx"; }); } var newCode = $(this).val() code1223 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1223!=newCode){ // window.frames["iframe1223"].document.write("
"); // window.frames["iframe1223"].document.write(decodeHtml($("textarea#stepcodeTextarea1223").val())); // window.frames["iframe1223"].document.close(); // $(window.frames["iframe1223"]).load(function(){ // $("#iframe1223").height($("#iframe1223").contents().find("body").height()+showittryitheight); // }); // code1223 = newCode; // } }); $(".tryButton1223").click(function(){ $("#tryDiv1223").show(); $("#stepcodeTextarea1223").focus(); $("#stepcodeTextarea1223").height(200); $("#iframe1223").height(0); window.frames["iframe1223"].document.write("
"); window.frames["iframe1223"].document.write(decodeHtml($("textarea#stepcodeTextarea1223").val())); window.frames["iframe1223"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1223"]).load(function(){ $("#iframe1223").height($("#iframe1223").contents().find("body").height()+showittryitheight); }); $("#iframe1223").height($("#iframe1223").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1223.focus(); editor1223.setSize(null, "250"); $("#rendering1223").hide(); $("#rendered1223").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 editor1223 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1223"), { 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); } } }); editor1223.on("change",function(doc){ if(!binded1223){ $(window).bind('beforeunload',function(){ binded1223 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1223 = newCode; $("textarea#stepcodeTextarea1223").val(newCode); if(alreadyWriteCode1223!=code1223){ lastModifedTime1223 = new Date().getTime(); $("#rendering1223").show(); $("#rendered1223").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1223 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1223.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1223").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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


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


关于 前端部分-JQuery-属性 的提问

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

上传截图