Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery如何获取imgs src_Javascript_Jquery - Fatal编程技术网

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);
});