javascript 获取id

来源:百度知道 编辑:UC知道 时间:2024/06/23 05:38:33
<div id="img" style="position:absolute; width:100px; height:80px; left:10px; top:5; ">
<img width="100" height="80" id="id1" src="images/_KF[HIA5TB~@SC7[~2U$[KR.jpg" />
</div>

<div id="img1" style="position:absolute; width:100px; height:80px; left:9px; top:89px; ">
<img width="100" height="80" id="id2" src="images/1.jpg" onmouseover="" /></div>

<div id="img2" style="position:absolute; width:100px; height:80px; left:9px; top:171px; ">
<img width="100" height="80" id="id3" src="images/2.jpg" onmouseover="" /> </div>

当把鼠标放到图片上时怎么循环获得不同图片的ID??用这document.getElementById("img2");只能单独获得,图片数量多就太麻烦

恩。楼上正解。。。
var imgs=document.getElementsByTag(img);
for(var i=0 ;i<imgs.length;i++){
alert(imgs[i].id);
}

给你写了个例子 看看吧!! 鼠标放上 移开 点击时的各种变化,绝对适合你的!!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
.bg{ width:100px; height:100px;background: #000; border:1px solid #f00; margin-top:10px;}
</style>
<script language="javascript">
function changeW()
{