Javascript jquery如何获取imgs src
我有一个画廊,我有一些这样的代码设置Javascript jquery如何获取imgs src,javascript,jquery,Javascript,Jquery,我有一个画廊,我有一些这样的代码设置 $('.gallery img').click(function(){ $('#gal_img').html('<img src=""/>'); }); $('.gallery img')。单击(函数(){ $('#gal_img').html(''); }); 这有很多其他运行的东西不重要,但我希望它能从“.gallery img”中获得img src,并将其放入新的img中。我在.gallery div中有多个图像您可以使用以下任
$('.gallery img').click(function(){
$('#gal_img').html('<img src=""/>');
});
$('.gallery img')。单击(函数(){
$('#gal_img').html('');
});
这有很多其他运行的东西不重要,但我希望它能从“.gallery img”中获得img src,并将其放入新的img中。我在.gallery div中有多个图像您可以使用以下任一选项:
$('.gallery img').click(function(){
var src = $(this).attr('src');
$('#gal_img').html('<img src="' + src + '"/>');
});
作为@Vision建议,您也可以使用纯javascript
this.src
$('.gallery img').click(function () {
$('#gal_img').html('<img src="' + this.src + '"/>');
});
$('.gallery img')。单击(函数(){
$('#gal_img').html('');
});
您也可以使用
$('img').prop('src')
或者,您可以简单地尝试以下方法:
$('.gallery img').click(function(){
$('#gal_img').html('<img src="' + this.src + '"/>');
});
$('.gallery img')。单击(函数(){
$('#gal_img').html('');
});
您也可以这样做(最快的解决方案):
$('.gallery img')。单击(函数(){
$('#gal_img').html('');
});
您可以执行以下操作:
$('.gallery img').click(function(){
var img = $('<img />', {'src' : this.src});
$('#gal_img').html(img);
});
$('.gallery img')。单击(函数(){
var img=$(“他希望它从“.gallery img”中获取img src”这不是一种权利answer@sudhar使用prop
是最好的方法。这个答案是正确的。不需要过于复杂。这个.src
是最快的解决方案。事实上,我把它作为建议写在我的答案旁边,但最后删除了它,因为我害怕用错误的方式应用它:P
$('.gallery img').click(function(){
$('#gal_img').html('<img src="' + this.src + '"/>');
});
$('.gallery img').click(function(){
var img = $('<img />', {'src' : this.src});
$('#gal_img').html(img);
});