簡體   English   中英

jquery懸停沒有顯示鏈接

[英]jquery hover not showing link

當我將鼠標懸停在圖像上時,不明白為什么我的鏈接不會顯示。 我確信這是非常簡單的,我現在無法看到。 如果有人指出我錯過了什么,以及可能有的任何其他提示,我將非常感激。

$("#profile_pic").hover(
        function () {
            $(this).show($('<a id="duh" href="upload.php">Change the picture</a>'));
        }
);

我試圖在懸停時顯示的鏈接如下

<div>
    <img id= "profile_pic" src="<?php echo $picture;?>" width="164" height="164" />
</div>

可能問題是<img>元素不能包含內容,但是你試圖將該標記附加到它上面。

您可以做的是將其附加到父級。

$("#profile_pic").hover(
        function () {
            $(this).closest('div').append($('<a id="duh" href="upload.php">Change the picture</a>'));
        });

現在,無論如何,請注意.hover() API將導致每次鼠標.hover()或移出元素時都會調用函數。 每個調用都會附加另一個 HTML塊。 你真的想這么做嗎?

相反,我建議您在頁面上始終使用<a>標記,並使.hover()處理程序顯示/隱藏它。

你不能向img添加任何內容,但你可以在你的img旁邊插入鏈接。

$("#profile_pic").hover(
   function () {
    var link = $("#duh");
    link = link.length ? link : $('<a id="duh" href="upload.php">Change the picture</a>');
    $(this).after(link);
   });

你能不能只將id移到包裝div上? 或者給包裝器自己的類或id?

<div id="profile_pic">
    <img src="<?php echo $picture;?>" width="164" height="164" />
</div>​

$("#profile_pic").hover(
    function () {
    $(this).append($('<a id="duh" href="upload.php">Change the picture</a>'));
 });​
$("#profile_pic").hover(
            function () {
                $(this).after($('<a id="duh" href="upload.php">Change the picture</a>'));
});
$("#profile_pic").hover(function () {
    $('<a id="duh" href="upload.php">Change the picture</a>').insertAfter(this);
});

我使用了insertAfter,因為我沒有知道你的其他結構,並且附加到父/包裝器可能不會將它放在圖像附近,而這應該是。 如果需要,您還可以使用$(this).parent()選擇器進行追加。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM