主题:
3024-night
ambiance-mobile
ambiance
base16-dark
blackboard
cobalt
eclipse
erlang-dark
lesser-dark
mbo
mdn-like
midnight
monokai
neat
neo
night
paraiso-dark
pastel-on-dark
rubyblue
solarized
the-matrix
tomorrow-night-eighties
vibrant-ink
新窗口
视图:
左侧面板
HTML
CSS
JS
预览
F11(全屏显示选中的编辑器)
查看更多帮助信息
相关文章:
jQuery.attr() 函数详解
代码类型:
默认(HTML/CSS/JS)
HTML混合(单页)
PHP
Java
C#
C++
C
SQL
杂项:
启用CSS/JS语法检查
监测脚本运行错误
禁止库文件重复
文档类型:
HTML5
HTML 4.01 Strict
HTML 4.01 Transitional
HTML 4.01 Frameset
XHTML 1.0 Strict
XHTML 1.0 Transitional
XHTML 1.0 Frameset
无
JS库文件:
无
jQuery 1.11.1
jQuery 2.1.1
jQuery 1.10.2
jQuery 1.9.1
jQuery 1.8.3
jQuery 1.7.2
jQuery 1.6.4
jQuery 1.5.2
jQuery 1.4.4
jQuery 1.3.2
jQuery 1.2.6
手动添加:
jQuery 1.11.1
<div id="n1"> <p id="n2" data-id="25">CodePlayer</p> <input id="n3" name="name" type="text" label="姓名" > <img id="n4" alt="CodePlayer" src="/image/blank.gif" > <img id="n5" alt="站点logo" title="专注于编程开发技术分享" src="/static/upload/image/info.png" > <ul id="n6"> <li id="n7" uid="21">item1</li> <li id="n8" uid="23">item2</li> <li id="n9" uid="35">item3</li> </ul> </div>
//设置n4(img元素)的src属性值 $("#n4").attr("src", "/static/upload/image/js-confirm-close-window-1.png"); var $n2 = $("#n2"); // 获取n2的data-id属性的值 document.writeln( $n2.attr("data-id") ); // 25 // 获取n2的myAttr属性的值,没有该属性,返回undefined document.writeln( $n2.attr("myAttr") ); // undefined //只返回第一个匹配元素的uid属性的值 document.writeln( $("li").attr("uid") ); // 21 // 以对象形式同时设置所有img元素的多个属性值 $("img").attr( { height: 180, width: 180, "class": "img-box" } ); document.writeln( $("#n5").attr("height") ); // 180 // 设置所有img元素的title属性值: // 1.如果该元素已经有了title属性,则不作改变 // 2.如果该元素之前没有title属性,则设置title属性等于它的alt属性 $("img").attr("title", function(index, attrValue){ // 这里的this表示当前DOM元素 return attrValue== undefined ? this.alt : attrValue; });