简体   繁体   English

我如何获得图像? attr JQuery

[英]How do I get the image? attr JQuery

I've been trying to get an image src with JQuery for a while but it's still not happening ... 我一直在尝试使用JQuery获取图像src,但仍然没有发生...

$(document).ready(function(){

var modall = $(".myModal");
var img = $(".myImg");
var modalImg = $(".img01");
var captionText = $(".caption");
var span = $(".close-img");

    img.click(function(){
        modall.css( 'display', 'block' );
        modalImg.attr( 'src' );
        captionText.attr( 'alt' );
    });

    span.click(function(){
      modall.css( 'display', 'none' );
    });

}); });

This is the code I'm trying to get 'src' and 'alt' on the 'myImg' image. 这是我试图在“ myImg”图像上获取“ src”和“ alt”的代码。

        <img class="myImg" src="<?php print $row['simg5']; ?>" alt="<?php print $row['title']; ?>" width="300" height="200" />
    <div class="image myModal">
        <span class="close-img">&times;</span>
        <img class="image-content img01" >
        <div class="caption"></div>
    </div>

And this is the code that I embed the SQL image itself. 这是我嵌入SQL映像本身的代码。

How do I take 'src' to 'MyImg' and get the image? 如何将“ src”带到“ MyImg”并获取图像?

Thanks ! 谢谢 !

You're not actually setting values with the following lines: 您实际上并没有使用以下几行设置值:

modalImg.attr( 'src' );
captionText.attr( 'alt' );

You need to pass in the value you would like to change that attribute to, otherwise you just return the current value of that attribute. 您需要传递您想要将该属性更改为的值,否则您只需返回该属性的当前值即可。

You'll need to change this block like so to pass in the src/alt of the clicked img: 您需要像这样更改此块,以传递单击的img的src / alt:

img.click(function(){
    modall.css( 'display', 'block' );
    modalImg.attr('src', $(this).attr('src'));
    captionText.attr('alt', $(this).attr('alt'));
});

See here for more details: http://api.jquery.com/attr/ 有关更多详细信息,请参见此处: http : //api.jquery.com/attr/

Hope that helps. 希望能有所帮助。

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

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