<html><script>
function doit(sID){
var obj = event.srcElement;
var b = false;
while(obj.tagName!="BODY"){
if(obj.id && obj.id==sID){
b =true;
alert("I belong to tag " + obj.tagName + " : id = " + obj.id);
break;
}
    obj = obj.parentElement;
}
if(!b) alert("not found");
}
</script><body><div id="div1">
<form method="post">
<input name="Button1" type="button" value="button"  onclick="doit('div1');"/></form>
</div>
<div id="div2">
<form method="post">
<input name="Button2" type="button" value="button" onclick="doit('div2');"/></form>
</div></body></html>

解决方案 »

  1.   

    onclick="doit('div1');
    onclick="doit('div2');
    这不是已经知道了是属于哪个DIV了么?:)
      

  2.   

    i think you can use some js library like mochikit to resovle this problem,if you know one of input,you can use parentNode attribute to find their parents
      

  3.   

    不判断也一样呀
    <html>
    <script>
    function doit(sID){
    var obj = event.srcElement;
    var b = false;
    while(obj.tagName!="BODY"){
    if(obj.tagName=="DIV"){
    b =true;
    alert("I belong to tag " + obj.tagName + " : id = " + obj.id);
    }
        obj = obj.parentElement;
    }
    if(!b) alert("not found");
    }
    </script><body><div id="div1">
    <form method="post">
    <input name="Button1" type="button" value="button"  onclick="doit();"/></form>
    </div>
    <div id="div2">
    <form method="post">
    <input name="Button2" type="button" value="button" onclick="doit();"/></form>
    </div></body></html>