简体   繁体   English

Javascript将div包裹在/悬停时删除锚点

[英]Javascript wrap div in/remove anchor on hover

I have divs I need to wrap in an anchor tag with href="#" on hover and remove when the mouse leaves. 我有div,我需要在悬停时使用href =“#”包裹在锚标记中,并在鼠标离开时将其删除。

When you hover on ps_image it wrap ps_img with <a href="#">DIV HERE</a> Then unwrap when not hovering. 当您将鼠标悬停在ps_image上时,它会用<a href="#">DIV HERE</a>包装ps_img,然后在不悬停时将其展开。

<div class="ps_image">
    <div class="ps_img">
        <img src="albums/album1/thumb/thumb.jpg" alt="Dachshund Puppy Thumbnail"/>
    </div>
</div>

ps doesn't matter if it only wraps the the child div, but would be nice Basically I'm trying to get the cursor to be the index pointer, without it linking ps是否只包装子div都没关系,但是会很好的。基本上,我试图使光标成为索引指针,而没有链接

That sounds like a terrible idea, but... 这听起来像一个可怕的主意,但是...

$('.ps_image').each(function() {
    var psImg = $(this).find('.ps_img'),
        a;
    $(this).hover(function() {
        a = psImg.wrap('<a href="#" />').parent();
    }, function() {
        a.children().unwrap();
    });
});

jsFiddle . jsFiddle

However , you probably have a misunderstanding of CSS. 但是 ,您可能对CSS有误解。 If you want the image to create a cursor, add cursor: pointer to its CSS selector. 如果要图像创建光标,请添加cursor: pointer其CSS选择器的cursor: pointer

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

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