日期:2014-05-16  浏览次数:20397 次

关于Button和Div的从属性问题,也许不可能解决!
例如下面的HTML,怎样知道Button1属于Div1,Button2属于Div2?   可以用探出窗口提示。


<html>

<head>
</head>

<body>

<div   id= "div1 ">
<form   method= "post ">
<input   name= "Button1 "   type= "button "   value= "button "   /> </form>
</div>
<div     id= "div2 ">
<form   method= "post ">
<input   name= "Button2 "   type= "button "   value= "button "   /> </form>
</div>

</body>

</html>


------解决方案--------------------
<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>
------解决方案--------------------
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