如何用 dom 加载的 JavaScript 替换页面上所有空的 <img src=""> ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3268335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:41:44  来源:igfitidea点击:

How to replace all empty <img src=""> on a page with JavaScript on dom loaded?

javascripthtmldomreplace

提问by ChrisBenyamin

I need a function, that starts, when the DOM is loaded.

我需要一个函数,它在加载 DOM 时启动。

In my HTML Page are several empty Image Tags, like and I want to add an Image-Name into the src-property when everything is loaded to get something like

在我的 HTML 页面中有几个空的图像标签,比如我想在加载所有内容时将图像名称添加到 src-property 中以获得类似的内容

<img src="blank.jpg">.

Best wishes Chris

最好的祝福克里斯

回答by Pavel Strakhov

Construction <img src="">is invalid. Never use it.

施工<img src="">无效。永远不要使用它。

It's invalid because empty url means url to current page. But current page is HTML document, not image. Browser can make another query of current page and try to use it as image (see this page).

这是无效的,因为空 url 表示当前页面的 url。但是当前页面是 HTML 文档,而不是图像。浏览器可以再次查询当前页面并尝试将其用作图像(请参阅此页面)。

回答by Rodrick Chapman

function replaceSrc()
{

    var images = document.getElementsByTagName('img');

    for(var i = 0; i < images.length; i++)
    {
        var img = images[i];

        if(img.src.length == 0)
        {
            img.src = 'blank.jpg';
        }
    }
}


window.onload = replaceSrc;

OR if you want to add more than one handler for the event:

或者,如果您想为事件添加多个处理程序:

document.addEventListener('load', replaceSrc, false) //W3C
document.attachEvent('onload', replaceSrc); //IE

With jQuery

使用 jQuery

   $(document)
     .ready(function() { $('img')
                             .filter(function(){ return this.src.length == 0 })
                                 .each(function () { this.src = 'blank.jpg'})  });

EDIT:

编辑:

I realized that you probably want to set the src property before the images load so I changed the code to fire on the document's load event, which happens before the images start loading.

我意识到您可能希望在图像加载之前设置 src 属性,因此我更改了代码以触发文档的加载事件,该事件在图像开始加载之前发生。

回答by advait

Use jQuery! It's easy.

使用jQuery!这简单。

$('img').attr('src','new-image-name.jpg')

This will set the srcattribute of every imgtag to 'new-image-name.jpg'.

这会将src每个img标签的属性设置为“new-image-name.jpg”。

回答by d4nt

With jQuery, this would set the src attribute of all img elements with a blank src attribute to "blank.jpg" on page load.

使用jQuery,这将在页面加载时将所有具有空白 src 属性的 img 元素的 src 属性设置为“blank.jpg”。

$.ready(function() {
  $("img[src='']").attr("src", "blank.jpg");
});

回答by P M

You can use this script.

您可以使用此脚本。

# Script AddImage.txt
var string pagefile, image, html
# Read in the page file.
cat $pagefile > $html
# Replace all instances of "<img src="blank.jpg">" with the specified image.
while ( { sen -r -c "^<img src=&\>^" $html } > 0 )
    sal -r -c "^<img src=&\>^" ("<img src=\""+$image+"\">") $html > null
# Write page back.
echo $html > { echo $pagefile }

Script is in biterscripting. Save the script in file "C:/Scripts/AddImage.txt", run it with this command.

脚本是biterscripting。将脚本保存在文件“C:/Scripts/AddImage.txt”中,使用此命令运行它。

script "C:/Scripts/AddImage.txt" pagefile("/path/to/page.html") image("blank.jpg")

It will replace previous image with "blank.jpg".

它将用“blank.jpg”替换之前的图像。

回答by Shamshad Zaheer

With JQuery you can simply solve your problem using following code.

使用 JQuery,您可以使用以下代码简单地解决您的问题。

$(function(){
   $("img[src='']").attr("src", "blank.jpg");
});