jQuery manipula...LOGIN

jQuery manipulating element attributes

jQuery operates element attributes

We can use getAttribute and setAttribute in javascript to operate the "element attributes" of the element.

In jQuery provides you with the attr() wrapping set function, which can simultaneously operate the attributes of all elements in the wrapping set:

QQ截图20161026094540.png

When using the id selector, only one object is often returned. jQuery wrapper set, at this time the attr(name) function is often used to obtain its element attributes:

function testAttr1(event) {    
         alert($("#hibiscus").attr("class"));    
    }

Note that the attr(name) function only returns the specific element attribute value of the first matching element. And attr(key , name) will set the element attributes of all packaging sets:

//修改所有img元素的alt属性    
$("img").attr("alt", "修改后的alt属性");

and attr( properties ) can modify multiple element attributes at one time:

  $("img").attr({title:"修改后的title", alt: "同时修改alt属性"});

In addition, although we can use removeAttr( name ) delete element attributes, but the corresponding DOM attributes will not be deleted, and will only affect the value of the DOM attribute.

For example, removing the readonly element attribute of an input element will cause the corresponding DOM attribute to become false (that is, the input becomes editable):

   $("#inputTest").removeAttr("readonly");


Next Section
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Jquery 动态修改连接</title> <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script> <!--上面更换您的jquery 路径--> </head> <style> span{display:block; width:100px; margin:0 20px 15px 0; text-align:center; height:20px; line-height:20px; display:block; float:left; background:#CCC;} div{border:#000 solid 1px; height:30px; clear:left; width:300px; line-height:30px; text-align:center;} </style> <script> var link1 = "<a href='http://www.baidu.com' target='_blank'>";//声明一个变量 var link2 = "<a href='http://www.qq.com' target='_blank'>"; var link3 = "<a href='http://www.sina.com.cn' target='_blank'>"; var linkR = "</a>";//连接结束 $(function(){ $("#link1").hover(function(){ $("#test").html(link1+"连接文字"+linkR);//修改指向时的连接 }) $("#link2").hover(function(){ $("#test").html(link2+"连接文字2"+linkR); }) $("#link3").hover(function(){ $("#test").html(link3+"连接文字3"+linkR); }) }) </script> <body> <span id="link1">1</span> <span id="link2">2</span> <span id="link3">3</span> <div id="test"></div> </body> </html>
submitReset Code
ChapterCourseware