定義和用法
attr() 方法設置或返回被選元素的屬性值。
根據該方法不同的參數,其工作方式也有所差異。
實例1
設置被選元素的屬性和值。
<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>
實例2
返回被選元素的屬性值。
<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>
以上所述就是本文的全部內容了,希望大家能夠喜歡。
新聞熱點
疑難解答