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

时间:2015-05-02 07:10:43

标签: html ios css uiwebview

就像

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

src不是正确的网址,如何使用所有图片的文件属性中的网址加载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>

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

3 个答案:

答案 0 :(得分:0)

使用jQuery编辑所有图像:

&#13;
&#13;
$(function() {    
  $("img").each(function(){
    var filepath = $(this).attr("file");
    if (filepath) this.src=filepath; 
  }); 
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img src="wrongsrc" file="http://www.hi-pda.com/forum/uc_server/data/avatar/000/52/56/39_avatar_middle.jpg"></img>
  <img src="wrongsrc" file="http://www.hi-pda.com/forum/uc_server/data/avatar/000/52/56/39_avatar_middle.jpg"></img>
  <img src="wrongsrc" file="http://www.hi-pda.com/forum/uc_server/data/avatar/000/52/56/39_avatar_middle.jpg"></img>
  <img src="wrongsrc" file="http://www.hi-pda.com/forum/uc_server/data/avatar/000/52/56/39_avatar_middle.jpg"></img>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

为每个图片添加ID

对于HTML

<img SRC="somesource" id="1" />
<img SRC="somesource" id="2" />
<img SRC="somesource" id="3" />

适用于JavaScript

(document).ready(function (){
$("img").each(function() {
var id = $(this).attr('id')
$(this).attr('src', ''+id+'_image.png');
});
});

重命名您的图像,如1_image.PNG,2_image.PNG等

听起来很混乱,但可能会在移动设备上运行,告知是否有效,因此无法自行测试。

答案 2 :(得分:0)

请使用其他版本的jQuery,它会正常工作。

jQuery("img").each(function(){
  var filepath = jQuery(this).attr("file");
  if (filepath) this.src=filepath; 
}); 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<img src="wrongsrc" file="http://www.hi-pda.com/forum/uc_server/data/avatar/000/52/56/39_avatar_middle.jpg">

相关问题