jQuery获取图像src [英] jQuery get the image src

查看:78
本文介绍了jQuery获取图像src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望当我单击按钮时,可以获取特定的img src并在div类img-block块中显示img src.

I hope when I click the button, I can get the specific img src and show the img src in the div class img-block block.

HTML

<button class="button">Click</button>
<div class="img1">
    <img src="img.jpg" alt="">
</div>

<div class="img-block"></div>

CSS

.img-block{
    position: absolute;
    top:10%;
    right:10%;
    background-color: red;
    width: 500px;
    height: 500px;
}
.img1 img{
    width: 200px;
}

JS

$('.button').click(function(){
  var images = $('.img1 img').attr(src);
  alert(images);      
});

但是现在我面临的问题是获取img src.
因此,我使用Alert进行测试,结果是没有任何警报.

But now I face the problem is to get the img src.
So I use alert to make the test, the result is it alert nothing.

推荐答案

src应该用引号引起来:

src should be in quotes:

$('.img1 img').attr('src');

这篇关于jQuery获取图像src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆