jQuery的attr()方法和JavaScript的属性操作

简介: jQuery的attr()方法和JavaScript的属性操作
1. 区别和概念:
  • jQuery的attr
  • attr是一个jQuery方法,用于读取或设置HTML元素的属性值。
  • 它用于获取或设置HTML属性,例如srchreftitle等。
  • attr返回的值通常是属性的字符串表示。


  • JavaScript的DOM属性:
  • 在JavaScript中,你可以使用DOM API来访问和操作HTML元素的属性。
  • 你使用getAttributesetAttribute方法来实现类似的功能。
  • JavaScript的DOM属性可以是更复杂的数据类型,而不仅仅是字符串。


2. JavaScript属性操作的用法示例:
<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript Attribute Manipulation</title>
  </head>
  <body>
    <img id="myImage" src="image.jpg" alt="Sample Image" />
    <button id="changeImage">Change Image Source</button>
    <script>
      // 获取元素引用
      var image = document.getElementById("myImage");
      var button = document.getElementById("changeImage");
      // 读取属性值
      var srcValue = image.getAttribute("src");
      var altValue = image.getAttribute("alt");
      // 输出属性值
      console.log("Initial src value: " + srcValue);
      console.log("Initial alt value: " + altValue);
      // 设置属性值
      image.setAttribute("src", "newImage.jpg");
      image.setAttribute("alt", "New Image");
      // 输出修改后的属性值
      console.log("New src value: " + image.getAttribute("src"));
      console.log("New alt value: " + image.getAttribute("alt"));
      // 添加事件处理程序,以在按钮点击时修改属性
      button.addEventListener("click", function () {
        image.setAttribute("src", "updatedImage.jpg");
      });
    </script>
  </body>
</html>


3. jQuery属性操作的用法示例:
<!DOCTYPE html>
<html>
  <head>
    <title>jQuery attr() Method</title>
  </head>
  <body>
    <img id="myImage" src="image.jpg" alt="Sample Image" />
    <button id="changeImage">Change Image Source</button>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
      $(document).ready(function () {
        var $image = $("#myImage");
        var $button = $("#changeImage");
        // 1. 读取属性值
        var srcValue = $image.attr("src");
        var altValue = $image.attr("alt");
        console.log("Initial src value: " + srcValue);
        console.log("Initial alt value: " + altValue);
        // 2. 设置属性值
        $image.attr("src", "newImage.jpg");
        $image.attr("alt", "New Image");
        console.log("New src value: " + $image.attr("src"));
        console.log("New alt value: " + $image.attr("alt"));
        // 3. 设置多个属性值
        $image.attr({
          src: "anotherImage.jpg",
          alt: "Another Image",
        });
        console.log("Another src value: " + $image.attr("src"));
        console.log("Another alt value: " + $image.attr("alt"));
        // 4. 移除属性
        $image.removeAttr("alt");
        console.log("Alt attribute removed: " + $image.attr("alt"));
        // 5. 检查属性是否存在
        var hasAltAttribute = $image.attr("alt") !== undefined;
        console.log("Alt attribute exists: " + hasAltAttribute);
        // 6. 添加自定义属性
        $image.attr("data-custom", "Custom Data");
        console.log("Custom data attribute: " + $image.attr("data-custom"));
        // 7. 使用回调函数修改属性值
        $button.click(function () {
          $image.attr("src", function (i, oldValue) {
            return "updatedImage.jpg";
          });
        });
      });
    </script>
  </body>
</html>


总结
  1. 读取属性值: 使用attr("attributeName")来读取元素的属性值。
  2. 设置属性值: 使用attr("attributeName", "value")来设置元素的属性值。
  3. 设置多个属性值: 使用attr({ attributeName: "value" })来设置多个属性的值。
  4. 移除属性: 使用removeAttr("attributeName")来移除元素的属性。
  5. 检查属性是否存在: 使用attr("attributeName")来检查属性是否存在。
  6. 添加自定义属性: 使用attr("data-attribute", "value")来添加自定义数据属性。
  7. 使用回调函数修改属性值: 使用回调函数来根据属性的旧值来设置新值。
目录
相关文章
|
1天前
|
JavaScript API
js【最佳实践】遍历数组的八种方法(含数组遍历 API 的对比)for,forEach,for of,map,filter,reduce,every,some
js【最佳实践】遍历数组的八种方法(含数组遍历 API 的对比)for,forEach,for of,map,filter,reduce,every,some
9 1
|
5天前
|
JavaScript
js【详解】call()、apply()、bind()方法
js【详解】call()、apply()、bind()方法
20 6
|
3天前
|
JavaScript 前端开发
JavaScript中exec()方法详解
在这个示例中,exec()方法会找到两个匹配项,并打印出它们在字符串中的位置。
7 1
|
5天前
|
JavaScript 前端开发 容器
vue组件封装——固定宽高比的容器(2种方法:纯CSS实现 + JS实现)
vue组件封装——固定宽高比的容器(2种方法:纯CSS实现 + JS实现)
9 2
|
5天前
|
JavaScript
vue 全局响应键盘按键/监听键盘事件(含 js 获取键盘keyCode值的方法)
vue 全局响应键盘按键/监听键盘事件(含 js 获取键盘keyCode值的方法)
10 2
|
5天前
|
存储 JavaScript 前端开发
使用JavaScript的indexOf方法
使用JavaScript的indexOf方法
|
5天前
|
前端开发 JavaScript
js 等待接口访问成功后执行指定代码【3种方法】(含async await Promise的使用)
js 等待接口访问成功后执行指定代码【3种方法】(含async await Promise的使用)
6 1
|
5天前
|
JavaScript 前端开发 索引
JavaScript编码之路 【JavaScript之操作数组、字符串方法汇总】(三)
JavaScript编码之路 【JavaScript之操作数组、字符串方法汇总】(三)
10 1
|
2天前
|
JavaScript
JS数组操作---删除,arr.pop()方法从数组中删除最后一个元素,并返回该元素的值,arr.shift() 删除第一个值,arr.splice()方法,删除指定元素,arr.splice,从第一
JS数组操作---删除,arr.pop()方法从数组中删除最后一个元素,并返回该元素的值,arr.shift() 删除第一个值,arr.splice()方法,删除指定元素,arr.splice,从第一
|
2天前
|
JavaScript
Js,定义数组的方法,let 数组名 = [数据1,数据2,........,数据n],取值方法,数组名[x],arr[0],let sum sum = sum + arr[0],求和的写法,平均值
Js,定义数组的方法,let 数组名 = [数据1,数据2,........,数据n],取值方法,数组名[x],arr[0],let sum sum = sum + arr[0],求和的写法,平均值