attr()方法在jQuery中习惯于组or返回所选元素的属性和值。
语法如下:
- 要返回属性的值:
$(selector).attr(attribute)
- 设置属性和值:
$(selector).attr(attribute, value)
- 要使用函数设置属性和值:
$(selector).attr(attribute, function(index, currentvalue))
- 设置多个属性和值:
$(selector).attr({attribute:value, attribute:value, ...})
- 属性:此参数用于指定属性的名称
- 值:用于指定属性的值
- 函数(索引, 当前值):它用于指定一个函数, 该函数返回要设置的属性值
- 指数:借助此参数接收到的元素的索引位置。
- 当前值:它用于接收所选元素的当前属性值。
<
!DOCTYPE html>
<
html >
<
head >
<
title >
jQuery attr() Method
<
/ title >
<
script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
<
/ script >
<
/ head >
<
body >
<
center >
<
h1 style = "color:green;
" >
lsbin<
/ h1 >
<
h2 >
jQuery attr() Method<
/ h2 >
<
h3 style = "color:lightgreen;
" >
<
/ h3 >
<
img src =
"https://media.lsbin.org/wp-content/uploads/20190221153831/1132-120x300.png"
alt = ""
width = "120"
height = "300"
class = "alignnone size-medium wp-image-915678" />
<
br >
<
br >
<
button >
Click<
/ button >
<
script >
$(document).ready(function() {
$("button").click(function() {
$("img").attr("width", "300");
});
});
<
/ script >
<
/ center >
<
/ body >
<
/ html >
输出如下:
之前单击按钮:
文章图片
单击按钮后:
文章图片
示例2:
<
!DOCTYPE html>
<
html >
<
head >
<
title >
jQuery attr() Method<
/ title >
<
script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
<
/ script >
<
/ head >
<
body >
<
center >
<
h1 style = "color:green;
" >
lsbin<
/ h1 >
<
h2 >
jQuery attr() Method<
/ h2 >
<
h3 style = "color:lightgreen;
" >
<
/ h3 >
<
img src =
"https://media.lsbin.org/wp-content/uploads/20190221153831/1132-120x300.png"
alt = ""
width = "120"
height = "300"
class =
"alignnone size-medium wp-image-915678" />
<
br >
<
br >
<
button >
Click<
/ button >
<
script >
$(document).ready(function() {
$("button").click(function() {
alert("Image width: " +
$("img").attr("width"));
});
});
<
/ script >
<
/ center >
<
/ body >
<
/ html >
输出如下:
之前单击按钮:
文章图片
单击按钮后:
文章图片
示例3:
<
!DOCTYPE html>
<
html >
<
head >
<
title >
jQuery attr() Method<
/ title >
<
script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
<
/ script >
<
/ head >
<
body >
<
center >
<
h1 style = "color:green;
" >
lsbin<
/ h1 >
<
h2 >
jQuery attr() Method<
/ h2 >
<
h3 style = "color:lightgreen;
" >
<
/ h3 >
<
img src =
"https://media.lsbin.org/wp-content/uploads/20190221153831/1132.png"
alt = ""
width = "120"
height = "300"
class =
"alignnone size-medium wp-image-915678" />
<
br >
<
br >
<
button >
Click<
/ button >
<
script >
$(document).ready(function() {
$("button").click(function() {
$("img").attr("width", function(n, v) {
return v - 50;
});
});
});
<
/ script >
<
/ center >
<
/ body >
<
/ html >
输出如下:
之前单击按钮:
文章图片
单击按钮后:
文章图片
示例4:
<
!DOCTYPE html>
<
html >
<
head >
<
title >
jQuery attr() Method
<
/ title >
<
script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
<
/ script >
<
/ head >
<
body >
<
center >
<
h1 style =
"color:green;
" >
lsbin<
/ h1 >
<
h2 >
jQuery attr() Method<
/ h2 >
<
h3 style = "color:lightgreen;
" >
<
/ h3 >
<
img src =
"https://media.lsbin.org/wp-content/uploads/20190221153831/1132.png"
alt = ""
width = "120"
height = "300"
class =
"alignnone size-medium wp-image-915678" />
<
br >
<
br >
<
button >
Click<
/ button >
<
script >
$(document).ready(function() {
$("button").click(function() {
$("img").attr({
width: "150", height: "100"
});
});
});
<
/ script >
<
/ center >
<
/ body >
<
/ html >
输出如下:
之前单击按钮:
文章图片
【jQuery attr()方法用法介绍和代码示例】单击按钮后:
文章图片
推荐阅读
- 学习ReactJS之前必须了解的5大技能
- 算法设计(查找数组中数字的出现频率)
- 使用C++ STL中的Set计算右侧较小的元素
- Python focus_set()和focus_get()方法用法介绍
- Amazon SDE实习面试经验
- 信息安全实现方法指南
- 宏碁win7 64位旗舰装机版最新系统推荐
- windows7 64位Ghost旗舰版最新系统推荐
- 戴尔笔记本WIN7 32位旗舰版最新系统推荐