用过Dtree的请进。

zhgwbzhd 2012-08-20 05:08:40
我的代码如下:

<!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=utf-8" />
<title>Dtree</title>

<link rel="StyleSheet" href="js/dtree/dtree.css" type="text/css" />
<script type="text/javascript" src="js/dtree/dtree.js"></script>

</head>

<body>
<div class="dtree">
<script type="text/javascript">
<!--

MY_DTree = new dTree('MY_DTree');//创建树,名称为'MY_DTree',和树的对象变量名必须一致。

alert(MY_DTree);
//alert(MY_DTree.toString());

document.write(MY_DTree);

//-->
</script>

</div>
</body>

</html>
----------------------------------------------------------
我的问题如下:
只是一个new dTree()。并且在他的js内的function dTree(objName)内也没发现有关其他的函数的调用。
此时 alert(MY_DTree)和alert(MY_DTree.toString())是一样的,有内容出现。就是
<div class="dtree">
</div>
也就是调用了函数
dTree.prototype.toString = function() {

var str = '<div class="dtree">\n';

if (document.getElementById) {
//只要用new dTree('树名'),就可以到这里。

if (this.config.useCookies) this.selectedNode = this.getSelected();

str += this.addNode(this.root);

} else str += 'Browser not supported.';

str += '</div>';

if (!this.selectedFound) this.selectedNode = null;

this.completed = true;

alert(str);

return str;

};

请问各位,什么时候调用的啊。
没看出来啊。

...全文
132 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2012-08-21
  • 打赏
  • 举报
回复
document.write(这里需要字符串)。不进行toString能得到字符串吗?
zhgwbzhd 2012-08-21
  • 打赏
  • 举报
回复
谢谢各位。
目前我找到的资料,看来是需要执行的。
http://www.blogjava.net/zhanglongsr/archive/2009/08/13/291035.html
内容是:
今天在看完xtree.js代码后,发现一个问题。在执行javascript的document.write(obj)方法时,如果obj实现了prototype的toString()方法,document.wirte方法将能自动调用obj的toString()方法。代码如下:
/*---------- 树构造代码 ---------*/
if (document.getElementById) {
var tree = new WebFXTree('Root');
tree.setBehavior('classic');
var a = new WebFXTreeItem('1');
tree.add(a);
var b = new WebFXTreeItem('1.1');
a.add(b);
b.add(new WebFXTreeItem('1.1.1'));
b.add(new WebFXTreeItem('1.1.2'));
b.add(new WebFXTreeItem('1.1.3'));
var f = new WebFXTreeItem('1.1.4');
b.add(f);

document.write(tree);

/*------------------ xtree.js代码片段 ----------*/

WebFXTree.prototype.toString = function() {
alert("it's here!");
var str = "<div id=\"" + this.id + "\" ondblclick=\"webFXTreeHandler.toggle(this);\" class=\"webfx-tree-item\" onkeydown=\"return webFXTreeHandler.keydown(this, event)\">" +
"<img id=\"" + this.id + "-icon\" class=\"webfx-tree-icon\" src=\"" + ((webFXTreeHandler.behavior == 'classic' && this.open)?this.openIcon:this.icon) + "\" onclick=\"webFXTreeHandler.select(this);\">" +
"<a href=\"" + this.action + "\" id=\"" + this.id + "-anchor\" onfocus=\"webFXTreeHandler.focus(this);\" onblur=\"webFXTreeHandler.blur(this);\"" +
(this.target ? " target=\"" + this.target + "\"" : "") +
">" + this.text + "</a></div>" +
"<div id=\"" + this.id + "-cont\" class=\"webfx-tree-container\" style=\"display: " + ((this.open)?'block':'none') + ";\">";
var sb = [];
for (var i = 0; i < this.childNodes.length; i++) {
sb[i] = this.childNodes[i].toString(i, this.childNodes.length);
}
this.rendered = true;
return str + sb.join("") + "</div>";
};

经过跟踪,证实在执行document.write(tree)时,调用了WebFXTree.prototype.toString 方法。
----------------------------------------------------------------------
另外,看到有资料说,js中每一个对象都有一个tostring。比如要输出整型等都可以输出字符串。
谢谢各位。
zhgwbzhd 2012-08-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
document.write(MY_DTree);
相当于
document.write(MY_DTree.toString());
[/Quote]

目前看来应该是的。
但是相关文档或者介绍有没??
谢谢!!
孟子E章 2012-08-20
  • 打赏
  • 举报
回复
document.write(MY_DTree);
相当于
document.write(MY_DTree.toString());
zhgwbzhd 2012-08-20
  • 打赏
  • 举报
回复
不对啊。哥们。
我把这两句都屏蔽了。
//alert(MY_DTree);
//alert(MY_DTree.toString());

你也发现了,在tostring函数内,有一个显示
alert(str);
即便现在,也会有一次的弹出框出现。
如果把alert(MY_DTree);放开的话,
一共会有三次弹出框弹出。

孟子E章 2012-08-20
  • 打赏
  • 举报
回复
d = new dTree("d");
d.add(0,-1,"<b>本站资源</b>","resources.aspx");
d.add(1,0,'语文','resources.aspx?type=new&subjectId=1');
d.add(2,0,'语文','resources.aspx?type=new&subjectId=1');
d.add(3,1,'xxxx','resources.aspx?type=new&subjectId=1');
d.add(4,1,'xxxx','resources.aspx?type=new&subjectId=1');
document.write(d);//document.write(d.toString());
未知数 2012-08-20
  • 打赏
  • 举报
回复
直接new dTree()是不会进入toString方法,只会执行构造函数
调用了进入该方法的语句是:
alert(MY_DTree);//这句会调用toString方法

87,901

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧