2012-04-19 57 views
0

我遇到了問題,我需要幫助。相對圖片源路徑使用jQuery返回null

我想創建一個使用jQuery的鼠標懸停動畫,以便當用戶將鼠標懸停在圖像上時,它會顯示圖像的突出顯示版本。我知道我可以用CSS做到這一點,但問題是內容需要管理。所以我想編寫一個匹配圖像文件名部分的函數,併爲文件名的其餘部分使用通配符。

這是HTML: 這是除非用戶將鼠標懸停在圖像上纔會顯示的圖像。

<img class="imgPath" src="<?php echo PATH_SITE. 'uploads/folder/bgr_img.jpg' ?>" alt="First Image" /> 

當在圖像的用戶懸停,我想的改變src:

<img class="imgPath" src="<?php echo PATH_SITE. 'uploads/folder/hvr_image.jpg' ?>" alt="First Image" /> 

鼠標移開時,我想在src恢復到以前的狀態「bgr_image.jpg」

這是我目前使用jQuery的:

$(function() { 
    $(".imgPath") 
     .mouseover(function() { 
      var src = $(this).attr("src").match("http://domain.com/path/to/img/hvr_image.jpg"); 
      $(this).attr("src", src); 
     }) 
     .mouseout(function() { 
      var src = $(this).attr("src").replace("http://domain.com/path/to/img/hvr_image.jpg", "http://domain.com/path/to/img/bgr_image.jpg"); 
      $(this).attr("src", src); 
     }); 
}); 

如果我將鼠標懸停在圖片,現在,它會將src更改爲「null」。我嘗試使用未包含域的路徑名,但它返回相同的值。

幫助將不勝感激。

回答

1

爲什麼你匹配你的src或更換呢?只要改變它:

$(function() { 
    $(".imgPath").hover(
     function() { $(this).attr("src", "http://domain.com/path/to/img/hvr_image.jpg"); }, 
     function() { $(this).attr("src", "http://domain.com/path/to/img/bgr_image.jpg"); } 
    ); 
}); 

編輯:

匹配()返回匹配的數組:通過[0]

$(function() { 
    $(".imgPath") 
     .mouseover(function() { 
      var src = $(this).attr("src").match("http://domain.com/path/to/img/hvr_image.jpg"); 
      $(this).attr("src", src[0]); 
     }) 
     .mouseout(function() { 
      var src = $(this).attr("src").replace("http://domain.com/path/to/img/hvr_image.jpg", "http://domain.com/path/to/img/bgr_image.jpg"); 
      $(this).attr("src", src); 
     }); 
}); 

EDIT2你的src:

<img class="imgPath" onmouseover="changeImgSrc(this, '<?php echo PATH_SITE. 'uploads/folder/hvr_image.jpg') ?>'" onmouseout="changeImgSrc(this, '<?php echo PATH_SITE. 'uploads/folder/bgr_image.jpg') ?>'" src="<?php echo PATH_SITE. 'uploads/folder/bgr_image.jpg' ?>" alt="FirstImage" /> 

<script> 
    function changeImgSrc(img, imgsrc) { 
     $(img).attr('src', imgsrc); 
    } 
</script> 

OR:

<img class="imgPath" onmouseover="this.src = '<?php echo PATH_SITE. 'uploads/folder/hvr_image.jpg') ?>'" onmouseout="this.src = '<?php echo PATH_SITE. 'uploads/folder/bgr_image.jpg') ?>'" src="<?php echo PATH_SITE. 'uploads/folder/bgr_image.jpg' ?>" alt="FirstImage" /> 
+0

因爲我想使用通配符,以便我可以應用於該部分的所有圖像網站。這種方法只能應用於1個實例,我怎麼可以將它應用於5個不同名稱的圖像。寫5次的函數?似乎馬虎。 – 2012-04-19 08:29:17

+0

它仍然返回null。如果我在匹配函數中使用相同的圖像,那就加載了。它不會顯示任何錯誤,因爲存在匹配並且src不爲空。但是,如果嘗試將其與圖像進行懸停狀態相匹配,它會告訴我src爲空。 – 2012-04-19 08:47:37

+0

我在帖子中添加了一個新的解決方案(EDIT2)。也許這可以幫助你。 – WolvDev 2012-04-19 09:24:24

0

的「匹配」函數返回一個布爾值,而不是字符串(在鼠標懸停功能)

+0

否,「match」返回數組中的匹配項,如果不匹配則返回null。 – WolvDev 2012-04-19 08:34:59

+0

你是對的,我的js參考是錯誤的:( – fmgp 2012-04-19 08:37:56

+0

http://www.w3schools.com/jsref/ :) – WolvDev 2012-04-19 08:43:36

0

沒有必要使用匹配()替換()函數這個任務

做如下。

$(function() { 
    $(".imgPath") 
     .mouseover(function() { 

      $(this).attr("src", "http://domain.com/path/to/img/bgr_image.jpg"); 

     }) 
     .mouseout(function() { 
      $(this).attr("src", "http://domain.com/path/to/img/hvr_image.jpg"); 

     }); 
}); 

編輯:

讓說,如果你有這種情況的多個圖像:

<div class="imgPath"><img src="1.jpg" /> </div> 
<div class="imgPath"><img src="2.jpg" /></div> 
<div class="imgPath"><img src="3.jpg" /> </div> 
<div class="imgPath"><img src="4.jpg" /></div> 

因此,我們可以用下面的代碼管理此:

$('.imgPath img').hover(
    function(){ 
     $('.imgPath img').each(
      function(){ 
       $(this).attr("src", "http://domain.com/path/to/img/bgr_image.jpg"); 
      }); 
    }, 
    function(){ 
     $('.imgPath img').each(
      function(){ 
       $(this).attr("src", "http://domain.com/path/to/img/hvr_image.jpg"); 
      }); 
    } 

); 

我認爲這會幫助你礦石。

謝謝。

+0

是的,就像之前的答案一樣。不過,我想將其應用於多個圖像。使用這種方法,我只能將它應用於1張圖片。 – 2012-04-19 08:38:26

+0

請檢查我編輯的答案部分。謝謝 – Chandresh 2012-04-19 08:49:13