如何更换所有空的<;img src=”等&引用&燃气轮机;在加载了dom上的JavaScript的页面上?
我需要一个函数,它在加载DOM时启动 在我的HTML页面中有几个空的图像标记,比如,我想在加载所有内容时将图像名称添加到src属性中如何更换所有空的<;img src=”等&引用&燃气轮机;在加载了dom上的JavaScript的页面上?,javascript,html,dom,replace,Javascript,Html,Dom,Replace,我需要一个函数,它在加载DOM时启动 在我的HTML页面中有几个空的图像标记,比如,我想在加载所有内容时将图像名称添加到src属性中 <img src="blank.jpg">. 。 祝福 克里斯 使用jQuery $(document) .ready(function() { $('img') .filter(function(){ return this.src.length == 0 })
<img src="blank.jpg">.
。
祝福
克里斯
使用jQuery
$(document)
.ready(function() { $('img')
.filter(function(){ return this.src.length == 0 })
.each(function () { this.src = 'blank.jpg'}) });
编辑:
我意识到您可能希望在加载图像之前设置src属性,因此我将代码更改为在文档的加载事件中触发,该事件发生在图像开始加载之前。使用!这很容易
$('img').attr('src','new-image-name.jpg')
这会将每个img
标记的src
属性设置为“new image name.jpg”。使用,这会在页面加载时将所有具有空白src属性的img元素的src属性设置为“blank.jpg”
$.ready(function() {
$("img[src='']").attr("src", "blank.jpg");
});
构造
无效。永远不要使用它
无效,因为空url表示指向当前页面的url。但当前页面是HTML文档,而不是图像。浏览器可以对当前页面进行另一个查询,并尝试将其用作图像(请参阅)。您可以使用此脚本
# 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 }
它将以前的图像替换为“blank.jpg”。使用JQuery,您可以使用以下代码简单地解决问题
$(function(){
$("img[src='']").attr("src", "blank.jpg");
});
有没有不使用JQuery的理由?是的,我们不想让其他框架使项目负担过重。我们已经在广泛的prototypeJS.Yup中使用了。在最坏的情况下,这将触发对同一(主页)页面的多次调用。(达到每日投票限制,否则将+1)
# 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 "C:/Scripts/AddImage.txt" pagefile("/path/to/page.html") image("blank.jpg")
$(function(){
$("img[src='']").attr("src", "blank.jpg");
});