站內搜尋

Friday, May 18, 2012

[jQuery] 更換html標籤中的屬性的屬性值 (以換圖為例[更換img圖檔src為例])


<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("img").attr("src","http://michaellauhk.com/diary/wp-content/uploads/2011/06/apple-logo.jpg");
  });
});
</script>
</head>
<body>
<img src="http://www.newmobilelife.com/wp-content/uploads/2011/05/apple.jpg" />
<br />
<button>change</button>
</body>
</html>

說明:
使用attr(),第一個參數指定要控制哪個屬性,第二個參數則是屬性質。

No comments:

Post a Comment