2011-09-10 71 views
3

我想實現

我想div的背景圖片來改變上述的div內的鏈接的鼠標懸停什麼。更改背景圖片

<div id="container"> 
    <a href="" id="linktomouseover"></a> 
</div> 

我已經試過

我已經試過交換使用jQuery的背景圖像:

<script language="javascript" type="text/javascript"> 

jQuery('#linktomouseover').mouseover(function() 
{ 
jQuery('#container').css("background-image", "url(bg-b.png)"); 
}); 

</script> 

誰能告訴我對我缺少的是什麼嗎?

解決方案

<style> 
#container{ 
width:100%; 
height:100%; 
background-image:url(http://thumbs.dreamstime.com/thumblarge_536/12838649102C1cO5.jpg); 
} 
</style> 

<div id="container"><br><br> 
<a href="" id="linktomouseover">hover</a> 
</div> 

<script> 
jQuery('#linktomouseover').hover(function() 
{ 
jQuery('#container').css("background-image", 
"url(http://static4.depositphotos.com/1011237/285/i/450/dep_2853473-Background-for-your-web-store.jpg) 
"); 
}).mouseleave(function(){ 
jQuery('#container').css("background-image", "url(http://thumbs.dreamstime.com/thumblarge_536/12838649102C1cO5.jpg)"); 
}); 
</script> 

回答

5

缺少ID =

<div id="container"> 

<div="container"> 

和LSO開關。點擊()與.hover()作爲鏈接說

http://sandbox.phpcode.eu/g/31e7b/6

<style> 
#container{ 
     width:100%; 
    height:100%; 
    background-image:url(http://thumbs.dreamstime.com/thumblarge_536/12838649102C1cO5.jpg); 
} 
</style> 
<div id="container"><br><br> 
    <a href="" id="linktomouseover">hover</a> 
</div> 
<script> 
jQuery('#linktomouseover').hover(function() 
{ 
    jQuery('#container').css("background-image", "url(http://static4.depositphotos.com/1011237/285/i/450/dep_2853473-Background-for-your-web-store.jpg)"); 
}); 
</script> 
+0

你是對的:) – Rafay

+0

這不是刪除的原因:)但是我看到更多的錯誤:p – genesis

+0

點擊?我認爲你的意思是鼠標懸停或我錯過了什麼? –

1

這可能不是解決辦法,但DOM模型加載後,這可能意味着的jQuery(「#linktomouseover」)沒有返回您目前沒有運行你的代碼。此外

<script> 
jQuery(function() { 
    // Your code here 
}); 
</script> 

,如果你想有一個以上的鏈接這種效果,你可以像這樣做;:

試試這個

HTML:

<div class="container"> 
    <a class="linktomouseover">link</a> 
</div> 
<div class="container"> 
    <a class="linktomouseover">link</a> 
</div> 

JS:

jQuery(function() { 
    jQuery('.linktomouseover').mouseover(function() { 
     jQuery(this).parent().css('background-image', "url('bg-b.png')"); 
    }); 
}); 
+0

乾杯jQuery代碼。 –