admin管理员组文章数量:1356808
So I know that it is easy enough to swap images of named img tags
<img name="image1" src="" />
<script>document["image1"].src="candybar.jpg";</script>
The problem is that I am being forced to use content server, and I can't name the image tag.
So If I name a Div tag that wraps the image can I use that to specify the image tag in question?
Something like..
<div id="namedDiv"><img src="" /></div>
<script>
var imgDiv=document.getElementById['namedDiv'];
imgDiv.$imgtag$.src="candybar.jpg";
</script>
So because I know the parent, and it only has 1 image tage within, i want to say "hey Div, give me your only child as an object"
So I know that it is easy enough to swap images of named img tags
<img name="image1" src="" />
<script>document["image1"].src="candybar.jpg";</script>
The problem is that I am being forced to use content server, and I can't name the image tag.
So If I name a Div tag that wraps the image can I use that to specify the image tag in question?
Something like..
<div id="namedDiv"><img src="" /></div>
<script>
var imgDiv=document.getElementById['namedDiv'];
imgDiv.$imgtag$.src="candybar.jpg";
</script>
So because I know the parent, and it only has 1 image tage within, i want to say "hey Div, give me your only child as an object"
Share Improve this question asked Aug 31, 2009 at 14:55 EddieEddie 10.2k4 gold badges47 silver badges59 bronze badges 1- Based on djko's answer, here's my final solution [code] <script type="text/javascript"> //source div. var imgDiv=document.getElementById('hidImgDiv'); //target img. var eventBg=document.getElementById('eventDate'); eventBg.style.background='url('+imgDiv.childNodes[1].src+') no-repeat scroll 55% 90%'; </script> [/code] – Eddie Commented Aug 31, 2009 at 15:32
4 Answers
Reset to default 4Yes that follows, something like:
document.getElementById('namedDiv').getElementsByTagName('img')[0].src = 'mynewpath.jpg';
Every dom node have childNodes property which is an array of nodes. You can pick first one.
<script>
var imgDiv=document.getElementById['namedDiv'];
imgDiv.childNodes[0].src="candybar.jpg";
</script>
Depending on the markup and the browser, the <img>
element may not be the only child. For example, if there is any whitespace such as a line break then many browsers other than IE will create a text node.
The easiest way is to use the getElementsByTagName
method of the wrapper element, which returns a NodeList, and get the first element in that NodeList:
var div = document.getElementById("namedDiv");
var image = div.getElementsByTagName("img")[0];
image.src = "candybar.jpg";
You can shorten that if you don't mind making it slightly harder to follow:
document.getElementById("namedDiv").getElementsByTagName("img")[0].src = "candybar.jpg";
but that makes it a bit harder to debug when you make a mistake ;-)
why dont use css style and sprite for get the same resutl without javascript. ????
the idea: http://www.pixelovers./css-sprites-mejora-rendimiento-web-i-37249
I can explain it :)
本文标签: htmlAlternate means to get image src using JavaScriptStack Overflow
版权声明:本文标题:html - Alternate means to get image src using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744072652a2586170.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论