Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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更改所有图像的图像源_Javascript_Jquery - Fatal编程技术网

Javascript 使用jQuery更改所有图像的图像源

Javascript 使用jQuery更改所有图像的图像源,javascript,jquery,Javascript,Jquery,我的页面上有一些空图像标签,其中一个图像存储在数据属性中 在页面加载时,我想从数据属性中获取此值,并将其放置在源中 $('div').each('img').attr('src', $(this).data('src')); 这是正确的方法: $('div img').each(function(){ $(this).attr('src', $(this).data('src')); }); 演示:这是正确的方法: $('div img').each(function(){

我的页面上有一些空图像标签,其中一个图像存储在数据属性中

在页面加载时,我想从数据属性中获取此值,并将其放置在源中

$('div').each('img').attr('src', $(this).data('src'));

这是正确的方法:

$('div img').each(function(){
    $(this).attr('src', $(this).data('src'));    
});

演示:

这是正确的方法:

$('div img').each(function(){
    $(this).attr('src', $(this).data('src'));    
});
演示:

您没有正确使用。这里有一个使用示例

$('div img').each(function() {
   var $this = $(this);
   $this.attr('src', $this.data('src'));
})
你没有正确使用。这里有一个使用示例

$('div img').each(function() {
   var $this = $(this);
   $this.attr('src', $this.data('src'));
})

与使用
.each()

var images=document.querySelectorAll(“div img”),
imageCount=document.images.length;
对于(变量i=0;i

与使用
.each()

var images=document.querySelectorAll(“div img”),
imageCount=document.images.length;
对于(变量i=0;i