Code:
<div id="test"></div>
<script type="text/javascript">
var elem = document.getElementById("test");
elem.id = 1;
alert(elem.id);
</script>
Can I save elem and elem.id in array? I don't know how. I tried:
var elem[0] = document.getElementById("test");
elem[0].id = 1;
alert(elem[0].id);
You almost got it. First you need to create the array:
var elements = [];
Then you can put items in it:
elements[0] = document.getElementById("test");
elements[0].id = 1;
alert(elements[0].id);
You can also use the push method of Array objects. Also note: the id
property of an element is a string, and as such your number will be converted to the string "1"
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.