<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>
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>
onclick="doit('div2');
这不是已经知道了是属于哪个DIV了么?:)
<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>