|
jquery ѧϰ֮¶þ ÊôÐÔÏà¹Ø×ÊÁÏ£¬Ñ§Ï°jqueryµÄÅóÓÑ¿ÉÒԲο¼Ï¡£
attr(name) È¡µÃµÚÒ»¸öÆ¥ÅäÔªËصÄÊôÐÔÖµ¡£Í¨¹ýÕâ¸ö·½·¨¿ÉÒÔ·½±ãµØ´ÓµÚÒ»¸öÆ¥ÅäÔªËØÖлñÈ¡Ò»¸öÊôÐÔµÄÖµ¡£Èç¹ûÔªËØûÓÐÏàÓ¦ÊôÐÔ£¬Ôò·µ»Ø undefined ¡£ Access a property on the first matched element. This method makes it easy to retrieve a property value from the first matched element. If the element does not have an attribute with such a name, undefined is returned. ·µ»ØÖµ Object
²ÎÊý name (String) : ÊôÐÔÃû³Æ
ʾÀý ·µ»ØÎĵµÖеÚÒ»¸öͼÏñµÄsrcÊôÐÔÖµ¡£
HTML ´úÂë:
<img src="test.jpg"/> jQuery ´úÂë:
$("img").attr("src"); ½á¹û:
test.jpg --------------------------------------------------------------------------------------------------------------------------------------- attr(properties) ½«Ò»¸ö¡°Ãû/Öµ¡±ÐÎʽµÄ¶ÔÏóÉèÖÃΪËùÓÐÆ¥ÅäÔªËصÄÊôÐÔ¡£ ÕâÊÇÒ»ÖÖÔÚËùÓÐÆ¥ÅäÔªËØÖÐÅúÁ¿ÉèÖúܶàÊôÐÔµÄ×î¼Ñ·½Ê½¡£ ×¢Ò⣬Èç¹ûÄãÒªÉèÖöÔÏóµÄclassÊôÐÔ£¬Äã±ØÐëʹÓÃ'className' ×÷ΪÊôÐÔÃû¡£»òÕßÄã¿ÉÒÔÖ±½ÓʹÓÃ.addClass( class ) ºÍ .removeClass( class ). Set a key/value object as properties to all matched elements. This serves as the best way to set a large number of properties on all matched elements. Note that you must use 'className' as key if you want to set the class-Attribute. Or use .addClass( class ) or .removeClass( class ). ·µ»ØÖµ jQuery
²ÎÊý properties (Map) : ×÷ΪÊôÐԵġ°Ãû/Öµ¶Ô¡±¶ÔÏó
ʾÀý ΪËùÓÐͼÏñÉèÖÃsrcºÍaltÊôÐÔ¡£
HTML ´úÂë:
<img/> jQuery ´úÂë:
$("img").attr({ src: "test.jpg", alt: "Test Image" }); ½á¹û:
[ <img src= "test.jpg" alt:="Test Image" /> ] --------------------------------------------------------------------------------------------------------------------------------------- attr(key,value) ΪËùÓÐÆ¥ÅäµÄÔªËØÉèÖÃÒ»¸öÊôÐÔÖµ¡£ Set a single property to a value, on all matched elements. ·µ»ØÖµ jQuery
²ÎÊý key (String) : ÊôÐÔÃû³Æ
value (Object) : ÊôÐÔÖµ
ʾÀý ΪËùÓÐͼÏñÉèÖÃsrcÊôÐÔ¡£
HTML ´úÂë:
<img/> <img/> jQuery ´úÂë:
$("img").attr("src","test.jpg"); ½á¹û:
[ <img src= "test.jpg" /> , <img src= "test.jpg" /> ] --------------------------------------------------------------------------------------------------------------------------------------- attr(key,fn) ΪËùÓÐÆ¥ÅäµÄÔªËØÉèÖÃÒ»¸ö¼ÆËãµÄÊôÐÔÖµ¡£ ²»Ìṩֵ£¬¶øÊÇÌṩһ¸öº¯Êý£¬ÓÉÕâ¸öº¯Êý¼ÆËãµÄÖµ×÷ΪÊôÐÔÖµ¡£ Set a single property to a computed value, on all matched elements. Instead of supplying a string value as described 'above', a function is provided that computes the value. ·µ»ØÖµ jQuery
²ÎÊý key (String) : ÊôÐÔÃû³Æ
fn (Function) : ·µ»ØÖµµÄº¯Êý ·¶Î§:µ±Ç°ÔªËØ, ²ÎÊý: µ±Ç°ÔªËصÄË÷ÒýÖµ
ʾÀý °ÑsrcÊôÐÔµÄÖµÉèÖÃΪtitleÊôÐÔµÄÖµ¡£
HTML ´úÂë:
<img src="test.jpg"/> jQuery ´úÂë:
$("img").attr("title", function() { return this.src }); ½á¹û:
<img src="test.jpg" title="test.jpg" /> --------------------------------------------------------------------------------------------------------------------------------------- removeAttr(name) ´Óÿһ¸öÆ¥ÅäµÄÔªËØÖÐɾ³ýÒ»¸öÊôÐÔ Remove an attribute from each of the matched elements. ·µ»ØÖµ jQuery
²ÎÊý name (String) : Ҫɾ³ýµÄÊôÐÔÃû
ʾÀý ½«ÎĵµÖÐͼÏñµÄsrcÊôÐÔɾ³ý
HTML ´úÂë:
<img src="test.jpg"/> jQuery ´úÂë:
$("img").removeAttr("src"); ½á¹û:
[ <img /> ] |
|