我可以在html中更改img src吗?

我可以在html中更改img src吗?,html,ios,css,uiwebview,Html,Ios,Css,Uiwebview,就像 <img src="somesrc" file="filepath" /> src不是正确的url,如何使用文件中的url属性加载所有图像的img <!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery /jquery-1.4.min.js"></script&

就像

<img src="somesrc" file="filepath" />

src不是正确的url,如何使用文件中的url属性加载所有图像的img

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery
/jquery-1.4.min.js"></script>

</head>

<body>

    <img src="wrongsrc" file="http://www.hi-pda.com/forum/uc_server/data/avatar/000/52/56/39_avatar_middle.jpg"></img>

    <script >   
$("img").each(function(){
var filepath = $(this).prop("file");
if (filepath) this.src=filepath; 
}); 
</script>

</body>
</html>

$(“img”)。每个(函数(){
var filepath=$(this.prop(“文件”);
如果(filepath)this.src=filepath;
}); 

我将代码更改为此,但它不起作用。

使用jQuery编辑所有图像:

$(函数(){
$(“img”)。每个(函数(){
var filepath=$(this.attr(“文件”);
如果(filepath)this.src=filepath;
}); 
});

为每个图像添加一个id

用于HTML

<img SRC="somesource" id="1" />
<img SRC="somesource" id="2" />
<img SRC="somesource" id="3" />
重命名图像,如1_image.PNG、2_image.PNG等


这听起来令人困惑,但可能会在移动设备上工作,如果工作,请告知,因此无法自己测试它。

请使用另一个版本的jQuery,它将正常工作

jQuery(“img”)。每个(函数(){
var filepath=jQuery(this.attr(“文件”);
如果(filepath)this.src=filepath;
}); 

上面的答案是正确的,当我在电脑的chrome中运行它时,答案会变为正确。但是当我在ios中运行该代码时,在ios中使用jQuery似乎有一个小问题。可能我没有正确使用jQuery。我使用以下方法解决了我的问题:

    <script  type="text/javascript">
        var imgs = document.getElementsByTagName("img");
        for (var i = 0; i < imgs.length; i++) {
            if(imgs[i].getAttribute("file")!=null&&imgs[i].getAttribute("file")!=undefined){
                imgs[i].src=imgs[i].getAttribute("file");
            }
        }
    </script>

var imgs=document.getElementsByTagName(“img”);
对于(变量i=0;i
以字符串形式读取HTML文件,将url替换为您的url,并使用UIWebView的loadHTMLString函数使用最新字符串显示HTML。是的,我尝试了此方法,但速度稍慢。您可以选择以文件形式写入新的HTML,如果不更改,您可以稍后使用图像从本地读取。下一次运行会更快。我想更改所有img,而不仅仅是图像有id my_图像?我怎么做,谢谢你你说得对,只是更改了我的代码以获取文件属性。代码什么时候调用的?我在正文中有一个img:那个些脚本在标题中,但它不起作用?为什么?请将脚本包装在$(document).ready(function(){…})中,或者将脚本放在html的底部。在加载html之前运行脚本可能会导致不可预知的结果。我为您修复了它。其中有几个错误-缺少括号,这是$(this)如果你想要属性和道具是更好的方法。我想要更改所有img src属性,而不是只更改一个img。在for循环中将此代码添加到change ImgSelector中。你能告诉我为什么下面的html没有显示我想要的内容吗?我似乎在ios UIWebView中不支持jQuery,我该怎么做?你只需要更改“”在给定的代码中,url只能替换
jQuery(“img”).each(function(){var filepath=jQuery(this.attr(“file”);if(filepath)this.src=filepath;});
$(“img”).each(function(){var filepath=$(this.attr(“file”);if(filepath)this.src=filepath;})
谢谢。它在我的chrom中运行良好。但是在ios UIWebView中,它不工作?ios UIWebView有什么问题?最后一个问题是jQuery不能在用户创建的属性上使用prop。
    <script  type="text/javascript">
        var imgs = document.getElementsByTagName("img");
        for (var i = 0; i < imgs.length; i++) {
            if(imgs[i].getAttribute("file")!=null&&imgs[i].getAttribute("file")!=undefined){
                imgs[i].src=imgs[i].getAttribute("file");
            }
        }
    </script>