Blogger Information
Blog 61
fans 0
comment 0
visits 62828
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
jQuery对attr()属性与自定义属性的操作
Pengsir
Original
919 people have browsed it

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery对attr()属性与自定义属性的操作</title>
</head>
<body>
<img src="img9.jpg" alt="致青春" width="200px" height="200px" data="校园美女" id="pic">
</body>
<script src="../jquery/jquery-3.2.1.min.js"></script>
<script>
    //    1.attr()传入一个参数,表示获取元素属性,相当于读取器getter
    //    2.attr()传入二个参数,表示设置元素属性,相当于设置器setter
    //    3.attr()获取元素的自定义属性
    //    4.删除元素属性:removeAttr()
    //      5.attr()动态设置元素属性
    var at = $('img').attr('width')
    //        attr(),第二个参数支持回调函数
    //    $('#pic').attr('width',function () {
    //        return 200+1000
    //    })
    var at = $('img').attr('src')
    var at = $('img').attr('src', 'img7.jpg')
    var at = $('img').attr('data')
    var at = $('img').removeAttr('data')
    var at = $('img').attr('style', 'border-radius:50%;box-shadow:2px 2px 2px #888')
    var at = $('img').removeAttr('alt style')
    console.log(at)
</script>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

总结:

   1.attr()传入一个参数,表示获取元素属性,相当于读取器getter
   2.attr()传入二个参数,表示设置元素属性,相当于设置器setter
   3.attr()获取元素的自定义属性
    4.删除元素属性:removeAttr()

   5.attr()动态设置元素属性


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post