简体   繁体   English

如何将跨度插入图像

[英]How insert span into image

My goal is insert <span> into <image> , but I don't know how.我的目标是将<span>插入<image> ,但我不知道如何。 Similar effect is on page http://www.bulb.cz/cs/reference , but I don't want it on hover() but on click() .类似的效果在页面http://www.bulb.cz/cs/reference 上,但我不希望它在hover()而是在click() I want insert "data-title" into <span> and after that <span> show on <image> .我想在<span>插入“数据标题”,然后在<image>上显示<span> <image>

HTML: HTML:

 <div class="Fotogalery">   
 <img src="images/Thumb/1.jpg" data-title="Zdar"  /> 
 <img src="images/Thumb/2.jpg" data-title="Ahoj" />
 <img src="images/Thumb/3.jpg"  data-title="Cau"  />
 <img src="images/Thumb/4.jpg" data-title="KUK" />  
 <img src="images/Thumb/5.jpg" data-title="ohh" />  
 </div>

CSS: CSS:

.Foto{width: 25%;height:auto;float:left;box-sizing: border-box;}
.Gallery{position:relative;}
.Gallery span{position:absolute;z-index:999;}

Javascript: Javascript:

$(".Fotogalery img").addClass("Foto");
$(".Foto").wrap( "<div class='Gallery'></div>" );
$(".Foto").after("<span></span>");
$(".Foto").click(function() {
    var a = $(this).data('title');
});

Try this — http://jsfiddle.net/sergdenisov/p72jnbLt/5/ :试试这个——http : //jsfiddle.net/sergdenisov/p72jnbLt/5/

Javascript: Javascript:

$('.Fotogalery img').addClass('Foto');
$('.Foto').wrap('<div class="Gallery"></div>');
$('.Foto').click(function () {
    var a = $(this).data('title');
    $(this).after('<span>' + a + '</span>');
});

CSS: CSS:

.Fotogalery {
    font-size: 0;
}

.Foto {
    display: inline-block;
    width: 100%;
}

.Gallery {
    position: relative;
    display: inline-block;
    width: 25%;
}

.Gallery span {
    position: absolute;
    z-index: 1;
    left: 0;
    top: 0;
    font-size: 16px;
}

If i understand your question right, u can use next('span') to select span:如果我理解你的问题,你可以使用 next('span') 来选择跨度:

     $(".Foto").click(function() {
        var a = $(this).data('title');
        $(this).next('span').html(a);
  })

To have span floating over image, make span position:absolute , and container postion:relative要使 span 浮动在图像上,请使 span position:absolute和 container position:absolute postion:relative

http://jsfiddle.net/gbg1vfLb/2/ http://jsfiddle.net/gbg1vfLb/2/

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

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