关于自定义对象的问题

wo554006164 2010-01-08 10:16:19
StringBuffer.js

function StringBuffer(string)
{
this._string=new Array(string);
}
StringBuffer.prototype=
{
append:function(string)
{
this._string.push(string);
},
showString:function()
{
return this._string.join(",");
}
}

index.html

<!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=gb2312" />
<title>无标题文档</title>
<script type="text/javascript" src="StringBuffer.js">
var sb=new StringBuffer("第一");
function add()
{
var txtStr=document.getElementById("txtName").value;
sb.append(txtStr);
}
function show()
{
document.getElementById("divShow").value=sb.showString();
}
</script>
</head>

<body><form action="" method="get">
<input name="txtName" id="txtName" type="text" />
<input onclick="add()" type="button" value="add" />
<input onclick="show()" type="button" value="show" />
<div id="divShow"></div>
</form>
</body>
</html>

点击add时有错误:22行缺少对象
怎么会缺少对象呢?

...全文
84 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sohighthesky 2010-01-08
  • 打赏
  • 举报
回复
当script标签有src属性StringBuffer.js时

标签内的文本将忽略,此时也就是没有add方法,即缺少对象
yzone 2010-01-08
  • 打赏
  • 举报
回复
顶下
孟子E章 2010-01-08
  • 打赏
  • 举报
回复
说说解决过程,让别人学习
eduyu 2010-01-08
  • 打赏
  • 举报
回复
up
wo554006164 2010-01-08
  • 打赏
  • 举报
回复
来个人顶下,我好结帖
wo554006164 2010-01-08
  • 打赏
  • 举报
回复
我晕了,解决了
wo554006164 2010-01-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sohighthesky 的回复:]
当script标签有src属性StringBuffer.js时

标签内的文本将忽略,此时也就是没有add方法,即缺少对象
[/Quote]
正确

87,996

社区成员

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

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