2010-08-05 63 views
2

HTML的圖片src改變了jQuery的

<img id="storyimg" src="images/stor.jpg" alt="img" /> 
       <ul class="sb_menu">    
        <li><a href="linkpage.htm" class="newslink1">Wireless Networking at Fortune Inn, Noida</a></li> 
        <li><a href="linkpage.htm" class="newslink2">18th International Conference on Oral & Maxillofacial Surgery</a></li> 
        <li><a href="linkpage.htm" class="newslink3">WiFi deployment at Vellore Institute of Technology</a></li>       
       </ul> 

我想,當用戶在這些li項目我想改變形象喜歡 -

<script> 
       $('a.newslink1').bind('mouseover', function() { 
       $('img#storyimg').src("images/stor1.jpg"); 
...same for newslink2 and 3, image will be stor2 and 3 

移動,但這種不工作我想我寫了錯誤的jquery ?????????

回答

2
$('a.newslink1').bind('mouseover', function() { 
$('img#storyimg').attr("src","images/stor1.jpg"); 
1

你可能想$('img#storyimg').attr('src','path/to/new/image.jpg');

EDI T:JINX得拿我可樂! :o)

還有一件事,用.mouseenter()mouseleave()進行實驗。

4

您的代碼:

<script> 
    $('a.newslink1').bind('mouseover', function() { 
    $('img#storyimg').src("images/stor1.jpg"); 

錯誤:

線路3:使用 'ATTR',而不是像」 .attr( 「SRC」,「圖像/ stor1 'SRC'。 JPG「);」

第4行:'}); 「缺少在statment結束

正確的代碼:

<script> 
    $('a.newslink1').bind('mouseover', function() { 
    $('img#storyimg').attr("src","images/stor1.jpg"); 
    }); 

如果你想改變形象靠的鏈接,你可以編寫:

<img id="storyimg" src="images/stor.jpg" alt="img" /> 
<ul class="sb_menu">    
    <li><a href="linkpage.htm" class="newslink1" data-image="stor1.jpg">Wireless Networking at Fortune Inn, Noida</a></li> 
    <li><a href="linkpage.htm" class="newslink2" data-image="stor2.jpg">18th International Conference on Oral & Maxillofacial Surgery</a></li> 
    <li><a href="linkpage.htm" class="newslink3" data-image="stor3.jpg">WiFi deployment at Vellore Institute of Technology</a></li>       
</ul> 

<script> 
    //binds the mouseover-event-handler to all Links the are childs of an LI in UL with Class "sb_menu" 
    $('UL.sb_menu LI A').bind('mouseover', function(e) { 
    $('img#storyimg').attr("src","images/" + $(e.target).attr('data-image')); 
    }); 
</script> 

的提高:」 img#storyimg「作爲選擇器,但只有」#storyimg「變得更快,因爲getElementById(..)是本地瀏覽器功能。如果您使用「img#storyimg」,jquery必須請求getElementsByTagName('IMG')並遍歷列表以找到具有id「storyimg」的此元素。這需要很多時間,等於直接執行「getElementById」。 頁面中任何HTML元素的ID都必須是unice。請參閱:http://www.w3.org/TR/html401/struct/global.html#h-7.5.2(「此屬性爲元素分配一個名稱,該名稱在文檔中必須是唯一的。」)

1

我知道很久以前,問題已被提出,但也許有人可能需要其他解決方案。所以我想,也許我也可以幫忙。

你也可以使用「。懸停()」功能,也許是這樣的:

這一個<head></head>之間:

<script type="text/javascript"> 
    $(document).ready(function() { 
     var src_path = "path/images/"; 
     var src_suffix = ".jpg";     
     $('.yourclass').hover(       
      function() { 
      $(this).addClass("hover"); 
      var active_id = $(this).attr('id');  
      $('#' + active_id + '_pic').attr("src", src_path + active_id + '_big' + src_suffix); 
      }, 
      function() { 
      $(this).removeClass("hover"); 
      var active_id = $(this).attr('id'); 
      $('#' + active_id + '_pic').attr("src", src_path + active_id + '_small' + src_suffix); 
      } 
     ); 
    }); 
</script> 

而且<body></body>之間的一個:

<div class="fruits"> 
<a href="#" class="yourclass" id="apple"> 
<img id="apple_pic" src="files/images/apple_small.jpg" alt="apple" title="apple" /> 
</a> 
<!-- --> 
<a href="#" class="yourclass" id="pear"> 
<img id="pear_pic" src="files/images/pear_small.jpg" alt="pear" title="pear" /> 
</a> 
</div> 

在我們的網站之一,它

有關「.hover()」功能的更多信息,可以在這裏找到:http://api.jquery.com/hover/