js get by tag name not working
i want to resize all the images inside div test but its not working here the script and html
<script type="text/javascript">
function x(){
var yourdiv = document.getElementById('test');
var yourImg = decoument.getElementsByTagName('img');
yourImg.style.height = '400px';
yourImg.style.width = '300px';
}
</script>
<div id="test">
<img alt='' src='imges/book1.jpg' />
&l开发者_如何学运维t;img alt='' src='imges/book2.jpg' />
</div>
document.getElementsByTagName('img');
returns a node list not a single element
var yourImg = document.getElementsByTagName('img'); // both img tags in this list
yourImg[0].style.height = '400px';
yourImg[0].style.width = '300px';
List of issues
- Syntax error:
decoument
should bedocument
. - you are finding the
#test
element but never use it to filter the images inside it.. getElementsByTagName
return a list, so you need to iterate over the elements in that list..
All issues fixed with
function x(){
var yourdiv = document.getElementById('test');
var yourImg = yourdiv.getElementsByTagName('img'); // use yourdiv and not document to only search for images inside the yourdiv element
for (var counter = 0; counter < yourImg.length; counter++) { // loop the list of images
yourImg[counter].style.height = '400px';
yourImg[counter].style.width = '300px';
}
}
Demo at http://jsfiddle.net/gaby/dCSsP/
use array in js problem fixed..:) function over() {
var x=document.getElementById("p1");
alert(x.innerHTML);
var s = document.getElementsByTagName("h1");
alert(s[0].innerHTML);
s[0].innerHTML="changed";
x.innerHTML="text is change successfully ";
}
精彩评论