3 回答

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用JQuery給自定義屬性賦值取值
jQuery 屬性操作 - attr() 方法
定義和用法
attr() 方法設(shè)置或返回被選元素的屬性值。
一、返回屬性值
返回被選元素的屬性值。
語(yǔ)法
$(selector).attr(attribute)
參數(shù)描述
attribute 規(guī)定要獲取其值的屬性。
$(selector).attr(attribute)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ alert("Image width " + $("img").attr("width")); }); }); </script> </head> <body> <img src="/i/eg_smile.gif" width="128" height="128" /> <br /> <button>返回圖像的寬度</button> </body> </html> |
二、設(shè)置屬性/值
設(shè)置被選元素的屬性和值。
語(yǔ)法
$(selector).attr(attribute,value)
參數(shù)描述
attribute 規(guī)定屬性的名稱。
value 規(guī)定屬性的值。
$(selector).attr(attribute,value)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("img").attr("width","180"); }); }); </script> </head> <body> <img src="/i/eg_smile.gif" /> <br /> <button>設(shè)置圖像的 width 屬性</button> </body> </html> |
- 3 回答
- 0 關(guān)注
- 443 瀏覽
添加回答
舉報(bào)