问题描述
有类似这样的字段串:<p><imgsrc="/ueditor/net/upload/image/20151120/6358363963167809393441278.jpg"title="20150909031003_59_MovieDetailSmall.jpg"alt="20150909031003_59_MovieDetailSmall.jpg"/></p><p><imgsrc="/ueditor/net/upload/image/20151120/6358363963167809323232323.jpg"title="20150909031003_59_MovieDetailSmall.jpg"alt="20150909031003_59_MovieDetailSmall.jpg"/></p><p><imgsrc="/ueditor/net/upload/image/20151120/6358363963167801111111112.jpg"title="20150909031003_59_MovieDetailSmall.jpg"alt="20150909031003_59_MovieDetailSmall.jpg"/></p>
问题:怎样找到所有的img的src属性,然后在src前面加上ip地址.
解决方案
解决方案二:
varimglist=document.getelementbytagname("img");foreach(varimginimglist){img.src="ip"+img.src;}如果是jQuery就更简单了varimglist=$("img");foreach(varimginimglist){varsrc=$(img).attr("src");$(img).attr("src","ip"+src)}
解决方案三:
javascript、jquery都有这种功能,获取标签集合,然后遍历某个属性
解决方案四:
服务器端实现@{StringBuilderstr=newStringBuilder("<p><imgsrc="/ueditor/net/upload/image/20151120/6358363963167809393441278.jpg"title="20150909031003_59_MovieDetailSmall.jpg"alt="20150909031003_59_MovieDetailSmall.jpg"/></p>");str.Append("<p><imgsrc="/ueditor/net/upload/image/20151120/6358363963167809323232323.jpg"title="20150909031003_59_MovieDetailSmall.jpg"alt="20150909031003_59_MovieDetailSmall.jpg"/></p>");str.Append("<p><imgsrc="/ueditor/net/upload/image/20151120/6358363963167801111111112.jpg"title="20150909031003_59_MovieDetailSmall.jpg"alt="20150909031003_59_MovieDetailSmall.jpg"/></p>");Regexre=newRegex(@"src="".+?""",RegexOptions.IgnoreCase);MatchCollectionmc=re.Matches(str.ToString());foreach(varsinmc){@:*.*.*.*@s<br/>}}
解决方案五:
jquery遍历就搞定啦
解决方案六:
你给img增加一个统一class属性。比如叫class='myImg'<script>varip="192.168.1.1";varq=$('.myImg').each(function(){$(this).attr('src',ip+$(this).attr('src'));alert($(this).attr('src'));});</script>
解决方案七:
$("img").each(function(){varsrc=$(this).attr(str);$(this).attr(src,ip+src);})
解决方案八:
引用5楼hanjun0612的回复:
你给img增加一个统一class属性。比如叫class='myImg'<script>varip="192.168.1.1";varq=$('.myImg').each(function(){$(this).attr('src',ip+$(this).attr('src'));alert($(this).attr('src'));});</script>
你不是多此一举吗?直接$("img")多好
解决方案九:
引用7楼shugeer的回复:
Quote: 引用5楼hanjun0612的回复:
你给img增加一个统一class属性。比如叫class='myImg'<script>varip="192.168.1.1";varq=$('.myImg').each(function(){$(this).attr('src',ip+$(this).attr('src'));alert($(this).attr('src'));});</script>你不是多此一举吗?直接$("img")多好
这是习惯问题,我不清楚他是否存在其他不需要修改的img标签。
解决方案十:
引用6楼feng1366259807的回复:
$("img").each(function(){varsrc=$(this).attr(str);$(this).attr(src,ip+src);})
正解