我有HTML代码段:
<div class="main-photo" style="height: 304.875px;">
<img class="big-thumb" src="blob:http://localhost:8080/cf4ffcff-7322-44dc-8c46-3c29ef165378" style="top: -27px;">
</div>
鼠标悬停时我需要结果:
<div class="main-photo" style="height: 304.875px;">
<a class="fancybox fancybox.iframe" href="blob:http://localhost:8080/cf4ffcff-7322-44dc-8c46-3c29ef165378">
<img class="big-thumb" src="blob:http://localhost:8080/cf4ffcff-7322-44dc-8c46-3c29ef165378" style="top: -27px;">
</a>
</div>
我尝试这样的东西,当鼠标悬停在图像上时使用jQuery(存根构思):
$('.main-photo').hover(function() {
$('.main-photo ').append()..attr('class', 'fancybox fancybox.iframe');
});
但不行。
答案 0 :(得分:1)
你需要包装/解包锚元素,也最好从图像中动态地导出锚源
$('.main-photo').hover(function() {
var $img = $(this).find('img'),
$a = $('<a />', {
'class': 'fancybox fancybox.iframe',
href: $img.attr('src')
});
$img.wrap($a)
}, function() {
$(this).find('img').unwrap()
});
.main-photo {
border: 1px solid grey;
}
.fancybox {
border: 1px solid green;
background-color: lightblue;
display: inline-block;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="main-photo" style="height: 304.875px;">
<img class="big-thumb" src="//placehold.it/64X64" style="top: -27px;">
</div>
答案 1 :(得分:0)
您可能想尝试一下:
$('.main-photo').hover(function() {
$(this).find("img").wrap('<a class="fancybox fancybox.iframe" href="blob:http://localhost:8080/cf4ffcff-7322-44dc-8c46-3c29ef165378"></a>');
}, function(){
$(this).find('img').unwrap();
});
答案 2 :(得分:0)
$('.main-photo').hover(function() {
$('.main-photo ').append()..attr('class', 'fancybox fancybox.iframe');
});
尝试将您的代码更改为此
$('.main-photo').hover(function() {
$('.main-photo ').append("<a class="fancybox fancybox.iframe" href="blob:http://localhost:8080/cf4ffcff-7322-44dc-8c46-3c29ef165378">");
$(".big-thumb").append("</a>");
});