我的目標看起來很簡單。當鼠標進入圖像時,我希望圖像的尺寸擴大並變爲稍微不同的圖像版本。當鼠標離開圖像時,我希望它再次縮小並變回。我的代碼沒有完成這個,我不知道爲什麼。jQuery .html()不起作用
$(document).ready(function() {
$("#home").on("mouseenter", function() {
$("#home").animate({
width: "10%",
left: "4%",
top: "0%"
});
$("#home").html("<a href='home.php'><img src='image/home.png'></a>");
/*It works fine up 'til here, but after this point I get no response
even clicking the link doesn't work. If I comment out the previous line of
code, everything works as expected (excepting of course that the HTML
doesn't change). It is quite confounding - the next bit of code is almost
a duplicate, and it gives me no trouble at all!*/
});
$("#home").on("mouseleave", function() {
$("#home").animate({
width: "8%",
left: "5%",
top: "2%"
});
$("#home").html("<a href='home.php'><img src='image/d.png'></a>");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li id="home">
<a href='home.php'>
<img src="image/d.png">
</a>
</li>
只是出於好奇,爲什麼你在你找到時替換'li'的內容d只需更改'img'上的'src'? –
如果您想更改圖片來源,只需更改圖片來源。我認爲問題在於你用全新的DOM元素替換了id爲「home」的元素的整個DOM子樹。 (並且可能會在DOM樹從它下面被替換時干擾動畫) – dsh
@PaulAbbott沒有特別的理由,除了習慣。除非必須,否則我不急於做出改變。我已經有其他代碼對列表項進行其他操作,需要進行更改,我必須返回並重新計算才能調整圖像大小而不是其容器大小。如果這是最好的方法,我會做這些事情。 – Truth