我希望当我点击按钮时,我可以得到特定的img src,并在div类的img-block
块中显示img src。
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进行测试,结果是没有任何警告。
发布于 2013-11-13 02:33:02
src应包含在引号中:
$('.img1 img').attr('src');
发布于 2018-11-21 20:05:47
对于完整的url使用。
$('#imageContainerId').prop('src')
对于相对图像url,请使用
$('#imageContainerId').attr('src')
function showImgUrl(){
console.log('for full image url ' + $('#imageId').prop('src') );
console.log('for relative image url ' + $('#imageId').attr('src'));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id='imageId' src='images/image1.jpg' height='50px' width='50px'/>
<input type='button' onclick='showImgUrl()' value='click to see the url of the img' />
发布于 2017-04-27 15:36:01
您可能会找到likr
$('.class').find('tag').attr('src');
https://stackoverflow.com/questions/19937162
复制相似问题