zoukankan      html  css  js  c++  java
  • jQuery(八):属性操作

    一、获取或设置元素的属性值

    attr()获取或设置匹配元素的属性值,语法如下:

    获取元素属性值示例:

    <!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>

    效果:

    设置单个元素属性值示例:

    <!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>

    效果:

     

  • 相关阅读:
    Hibernate4学习day0--hibernate封装--注解--单元测试
    Hibernate4学习day01--简介--基本配置
    java基础day13---引用数据类型
    java基础day14---static关键字-----继承
    java基础day12---this 关键字-----参数传递
    day05 Struts2文件上传和下载---防重复提交
    java基础day11---空指针异常----引用类型--自定义类型赋值--封装
    java基础的第二轮快速学习!day10
    Struts2,大爷你好!第四天
    java基础的第二轮快速学习!day09
  • 原文地址:https://www.cnblogs.com/dotnet261010/p/9736107.html
Copyright © 2011-2022 走看看