3 回答

TA貢獻1808條經驗 獲得超4個贊
使用JQuery給自定義屬性賦值取值
jQuery 屬性操作 - attr() 方法
定義和用法
attr() 方法設置或返回被選元素的屬性值。
一、返回屬性值
返回被選元素的屬性值。
語法
$(selector).attr(attribute)
參數描述
attribute 規定要獲取其值的屬性。
$(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> |
二、設置屬性/值
設置被選元素的屬性和值。
語法
$(selector).attr(attribute,value)
參數描述
attribute 規定屬性的名稱。
value 規定屬性的值。
$(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>設置圖像的 width 屬性</button> </body> </html> |
- 3 回答
- 0 關注
- 454 瀏覽
添加回答
舉報