最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery操作屬性值方法介紹

 更新時間:2022年05月04日 11:39:03   作者:.NET開發(fā)菜鳥  
這篇文章介紹了jQuery操作屬性值的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、獲取或設(shè)置元素的屬性值

attr()獲取或設(shè)置匹配元素的屬性值,語法如下:

獲取元素屬性值示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>屬性操作</title>
    <style>
      *{
          margin: 0px;
          padding: 0px;
      }
      td{
          width: 100px;
          border: 1px solid #cccccc;
          cursor: pointer;
      }
    </style>
    <!--引入jQuery-->
    <script src="../jquery-3.3.1.js"></script>
    <!--javascript-->
    <script>
       $(function(){
           $("img").click(function(){
              // 獲取屬性的值
              alert($("img").attr("src")) ;              
           });          
       });
    </script>
</head>
<body>
    <img src="../qq.jpg" />
</body>
</html>

效果:

設(shè)置單個元素屬性值示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>屬性操作</title>
    <style>
      *{
          margin: 0px;
          padding: 0px;
      }
      td{
          width: 100px;
          border: 1px solid #cccccc;
          cursor: pointer;
      }
    </style>
    <!--引入jQuery-->
    <script src="../jquery-3.3.1.js"></script>
    <!--javascript-->
    <script>
       $(function(){
           $("img").click(function(){
              // 獲取屬性的值
              //alert($("img").attr("src")) ; 

              // 添加單個屬性
              $("img").attr("alt","QQ斗地主"); 
              alert($("img").attr("alt")) ;             
           });          
       });
    </script>
</head>
<body>
    <img src="../qq.jpg" />
</body>
</html>

效果:

添加多個屬性值示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>屬性操作</title>
    <style>
      *{
          margin: 0px;
          padding: 0px;
      }
      td{
          width: 100px;
          border: 1px solid #cccccc;
          cursor: pointer;
      }
    </style>
    <!--引入jQuery-->
    <script src="../jquery-3.3.1.js"></script>
    <!--javascript-->
    <script>
       $(function(){
           $("img").click(function(){
              // 獲取屬性的值
              //alert($("img").attr("src")) ; 

              // 添加單個屬性
              //$("img").attr("alt","QQ斗地主"); 
              //alert($("img").attr("alt")) ;

              // 添加多個屬性
              $("img").attr({"alt":"QQ斗地主","title":"斗地主"}); 
              console.log($(this).attr("alt")); 
              console.log($(this).attr("title"));            
           });          
       });
    </script>
</head>
<body>
    <img src="../qq.jpg" />
</body>
</html>

效果:

二、刪除屬性值

removeAttr()匹配的元素中刪除一個屬性,語法如下:

示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>屬性操作</title>
    <style>
      *{
          margin: 0px;
          padding: 0px;
      }
      td{
          width: 100px;
          border: 1px solid #cccccc;
          cursor: pointer;
      }
    </style>
    <!--引入jQuery-->
    <script src="../jquery-3.3.1.js"></script>
    <!--javascript-->
    <script>
       $(function(){
           $("img").click(function(){
              // 獲取屬性的值
              //alert($("img").attr("src")) ; 

              // 添加單個屬性
              //$("img").attr("alt","QQ斗地主"); 
              //alert($("img").attr("alt")) ;

              // 添加多個屬性
              //$("img").attr({"alt":"QQ斗地主","title":"斗地主"}); 
              //console.log($(this).attr("alt")); 
              //console.log($(this).attr("title")); 

              // 刪除屬性
              $(this).removeAttr("src");           
           });          
       });
    </script>
</head>
<body>
    <img src="../qq.jpg" />
</body>
</html>

效果:

到此這篇關(guān)于jQuery操作屬性值的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

剑阁县| 应城市| 瑞金市| 栖霞市| 霸州市| 庆元县| 南宁市| 犍为县| 罗平县| 尉犁县| 屏东县| 蛟河市| 石狮市| 隆回县| 湖口县| 龙江县| 东源县| 安仁县| 泰顺县| 格尔木市| 枝江市| 乾安县| 即墨市| 浠水县| 仪征市| 拉萨市| 江山市| 开远市| 木兰县| 志丹县| 兰西县| 梅州市| 攀枝花市| 秭归县| 南木林县| 杭锦后旗| 陆河县| 德令哈市| 虎林市| 化德县| 左云县|