简体   繁体   English

jquery悬停没有显示链接

[英]jquery hover not showing link

Not understanding why my link won't show up when I hover over an image. 当我将鼠标悬停在图像上时,不明白为什么我的链接不会显示。 I'm sure it is something very simple that I am unable to see at this time. 我确信这是非常简单的,我现在无法看到。 I would be very appreciative if someone pointed out what I'm missing as well as any other tips one may have. 如果有人指出我错过了什么,以及可能有的任何其他提示,我将非常感激。

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

The link I'm trying to get to show on hover is below 我试图在悬停时显示的链接如下

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

Probably the issue is that <img> elements can't have content, yet you're trying to append that markup to it. 可能问题是<img>元素不能包含内容,但是你试图将该标记附加到它上面。

What you could do is append that to the parent. 您可以做的是将其附加到父级。

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

Now, however you do this, note that the .hover() API will result in your function being called each time that the mouse moves in or out of the element. 现在,无论如何,请注意.hover() API将导致每次鼠标.hover()或移出元素时都会调用函数。 Each call will append another block of HTML. 每个调用都会附加另一个 HTML块。 Do you really want to do that? 你真的想这么做吗?

I suggest that, instead, you always have that <a> tag on the page, and that you make your .hover() handler show/hide it. 相反,我建议您在页面上始终使用<a>标记,并使.hover()处理程序显示/隐藏它。

You can't append anything to img, but you can insert link next to your img. 你不能向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);
   });

Can you not just move the id onto the wrapper div? 你能不能只将id移到包装div上? Or give the wrapper it's own class or id? 或者给包装器自己的类或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);
});

I used the insertAfter as I do not have knowlege of your other structure, and appending to the parent/wrapper might not put it in close proximity to the image whereas this should. 我使用了insertAfter,因为我没有知道你的其他结构,并且附加到父/包装器可能不会将它放在图像附近,而这应该是。 You could also use the $(this).parent() selector to append if needed. 如果需要,您还可以使用$(this).parent()选择器进行追加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM