样式: 在 style 里面写的, 用 CSS 来操作
属性: 在里面里面写的, 用 attr 方法操作
- // 设置单个属性
- //attr(name, value)
- $("img").attr("alt", "图破了");
- $("img").attr("title", "错错错错");
- // 设置多个属性
- $("img").attr({
- alt: "图破了",
- title: "错错错",
- aa: "bb"
- })
- console.log($("img").attr("alt"));
- <!DOCTYPE html>
- <HTML lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <title>
- Title
- </title>
- </head>
- <body>
- <img src="04.gif" alt="突破了" title="对对对">
- <script src="jquery-1.12.4.js">
- </script>
- <script>
- $(function() {
- // 设置多个属性
- $("img").attr({
- alt: "图破了",
- title: "错错错",
- aa: "bb"
- }) console.log($("img").attr("alt"));
- });
- </script>
- </body>
- </HTML>
来源: http://www.bubuko.com/infodetail-3384772.html