学习js困惑时,可以打开这个页面运行下,也许对你会有所帮助
现在看来这个尝试还需要改进。
<title>+ 页面对象属性方法列表 +</title>
<style>
div{border:1px solid red;}
#sty{border:0px;}
#tree {float:left;width:400;word-wrap:break-word;border:1px solid blue; background:#eeeeee;}
#right {float:right; position:absolute ;left:450px;border:1px solid blue; background:#eeeeee;}
#info {display:block;}
.bt {padding:0px;background:white;margin:0px;}
</style>
<div id="sty">按下 listtree 按钮。本页面只是对js的一次了解的尝试。</div>
<div id="tree">
</div>
<div id="right">
<A HREF="33" name="linktest">33</A>
<INPUT TYPE='button' onclick='showprop(document,"document")' value='listtree'>
<INPUT TYPE='button' onclick='showdiv(info)' value='showinfo'><BR>
标志:<INPUT TYPE="text" id="roottree" value="0">
<textarea cols='80' rows='20' name='info' id='info' ></textarea><BR>
<textarea cols='30' rows='5' name='evaltext' ></textarea><BR>
<textarea cols='30' rows='5' id='evalresult' ></textarea>
<INPUT TYPE='button' onclick='evalstr(evaltext)' value='eval'>
<BR>
</div>
<!-- script start -->
<script>
var s='';
var r='';
var i=1;
var rootdiv = 'tree';
var tmp='';
var tmpobject='';
var tmptext='';
//var tmp='';
function evalstr(obj){
res = eval(obj.value);
evalresult.value =res;
}
function showdiv(obj){
//alert(obj.id);
if(obj.style.display == 'none'){
obj.style.display = 'block';
document.getElementById(obj.id+'_bt').value='-';
}
else{
obj.style.display = 'none';
document.getElementById(obj.id+'_bt').value='+';
}
//alert(obj.style.display);
}
//<function showprop 取出指定对象的属性方法子对象。
function showprop(obj,objid)
{
//<if obj.innerhtml == ''>
if(1)
{
if (typeof(obj) == "undefined")
{
obj = eval(objid);
//alert(obj);
}
for(x in obj)
{
if (x==0){
alert('null object');
break;
}
//<else>
else {
node = objid +"." + x ;
nodevalue = eval(objid+"."+x); //nodevalue can be object or string
nodediv = "div_"+node;
re = /\./i;
nodediv = nodediv.replace(re,'_');
if(nodevalue == "[object]"){
link =nodevalue+'<a href=# onclick=\'showprop('+ node +',\"'+node+'\")\'>'+node+'</a>';
ctrlbutton = '<input type="button" class="bt" id="'+nodediv+'_bt" value="-" onclick="showdiv('+nodediv+')">';
tmpobject += ctrlbutton + link + '<div id="'+nodediv+'"></div>\n';
}
else{
tmptext += node +"="+nodevalue + "\n";
}
} // </else>
}//for 结束
tmp = tmpobject +tmptext;
//tmp = conv(tmp);
tmp = "<pre>"+tmp+"</pre>";
document.getElementById('info').value = '';
document.getElementById('info').value = tmp;
if(document.getElementById('roottree').value == '0'){
//rootdiv="tree";
document.getElementById(rootdiv).innerHTML = '';
document.getElementById(rootdiv).innerHTML = tmp;
//alert(rootdiv + typeof(rootdiv));
}
else{
subdiv = 'div_'+objid;
re = /\./i;
subdiv = subdiv.replace(re,'_');
//alert(subdiv);
document.getElementById(subdiv).innerHTML = tmp;
document.getElementById(subdiv).style.display = 'block' ;
document.getElementById(subdiv+'_bt').value='-';
}
document.getElementById('roottree').value = '1';
}//</if obj.innerhtml == ''>
tmp='';
tmpobject='';
tmptext='';
}
// </function showprop>
conv("<div></div>");
/*<function conv>*/
function conv(ss){
rea = /(<)/i;
reb = /(>)/i;// 创建正则表达式模式。
desta = "<";
destb = ">";
while(ss.search(rea) != -1) {ss = ss.replace(rea, desta); }
while(ss.search(reb) != -1) {ss = ss.replace(reb, destb); }
return(ss); // 返回替换后的字符串。
}
/*</function conv>*/
</script>
<style>
div{border:1px solid red;}
#sty{border:0px;}
#tree {float:left;width:400;word-wrap:break-word;border:1px solid blue; background:#eeeeee;}
#right {float:right; position:absolute ;left:450px;border:1px solid blue; background:#eeeeee;}
#info {display:block;}
.bt {padding:0px;background:white;margin:0px;}
</style>
<div id="sty">按下 listtree 按钮。本页面只是对js的一次了解的尝试。</div>
<div id="tree">
</div>
<div id="right">
<A HREF="33" name="linktest">33</A>
<INPUT TYPE='button' onclick='showprop(document,"document")' value='listtree'>
<INPUT TYPE='button' onclick='showdiv(info)' value='showinfo'><BR>
标志:<INPUT TYPE="text" id="roottree" value="0">
<textarea cols='80' rows='20' name='info' id='info' ></textarea><BR>
<textarea cols='30' rows='5' name='evaltext' ></textarea><BR>
<textarea cols='30' rows='5' id='evalresult' ></textarea>
<INPUT TYPE='button' onclick='evalstr(evaltext)' value='eval'>
<BR>
</div>
<!-- script start -->
<script>
var s='';
var r='';
var i=1;
var rootdiv = 'tree';
var tmp='';
var tmpobject='';
var tmptext='';
//var tmp='';
function evalstr(obj){
res = eval(obj.value);
evalresult.value =res;
}
function showdiv(obj){
//alert(obj.id);
if(obj.style.display == 'none'){
obj.style.display = 'block';
document.getElementById(obj.id+'_bt').value='-';
}
else{
obj.style.display = 'none';
document.getElementById(obj.id+'_bt').value='+';
}
//alert(obj.style.display);
}
//<function showprop 取出指定对象的属性方法子对象。
function showprop(obj,objid)
{
//<if obj.innerhtml == ''>
if(1)
{
if (typeof(obj) == "undefined")
{
obj = eval(objid);
//alert(obj);
}
for(x in obj)
{
if (x==0){
alert('null object');
break;
}
//<else>
else {
node = objid +"." + x ;
nodevalue = eval(objid+"."+x); //nodevalue can be object or string
nodediv = "div_"+node;
re = /\./i;
nodediv = nodediv.replace(re,'_');
if(nodevalue == "[object]"){
link =nodevalue+'<a href=# onclick=\'showprop('+ node +',\"'+node+'\")\'>'+node+'</a>';
ctrlbutton = '<input type="button" class="bt" id="'+nodediv+'_bt" value="-" onclick="showdiv('+nodediv+')">';
tmpobject += ctrlbutton + link + '<div id="'+nodediv+'"></div>\n';
}
else{
tmptext += node +"="+nodevalue + "\n";
}
} // </else>
}//for 结束
tmp = tmpobject +tmptext;
//tmp = conv(tmp);
tmp = "<pre>"+tmp+"</pre>";
document.getElementById('info').value = '';
document.getElementById('info').value = tmp;
if(document.getElementById('roottree').value == '0'){
//rootdiv="tree";
document.getElementById(rootdiv).innerHTML = '';
document.getElementById(rootdiv).innerHTML = tmp;
//alert(rootdiv + typeof(rootdiv));
}
else{
subdiv = 'div_'+objid;
re = /\./i;
subdiv = subdiv.replace(re,'_');
//alert(subdiv);
document.getElementById(subdiv).innerHTML = tmp;
document.getElementById(subdiv).style.display = 'block' ;
document.getElementById(subdiv+'_bt').value='-';
}
document.getElementById('roottree').value = '1';
}//</if obj.innerhtml == ''>
tmp='';
tmpobject='';
tmptext='';
}
// </function showprop>
conv("<div></div>");
/*<function conv>*/
function conv(ss){
rea = /(<)/i;
reb = /(>)/i;// 创建正则表达式模式。
desta = "<";
destb = ">";
while(ss.search(rea) != -1) {ss = ss.replace(rea, desta); }
while(ss.search(reb) != -1) {ss = ss.replace(reb, destb); }
return(ss); // 返回替换后的字符串。
}
/*</function conv>*/
</script>