********JavaScript中add是关键字吗?为什么我的input出错?

99666 2004-07-30 02:43:16
代码见下:
//-------------test.htm-----------------
<html>
<head>
<script language="javascript">
function add(){
alert("add");
}
</script>
</head>
<body>
<form>
<input type="button" id="add" value="Add" onClick="add()" />
</form>
<input type="button" id="add" value="Add" onClick="add()" />
</body>
</html>
//---------------------------------------------------
在IE中点击第一个按钮出错,说在第11行,对象不支持此属性或方法。
而第二个按钮没有问题。
如果改为<input type="button" id="add1" value="Add" onClick="add()" />
则没有问题,为什么?
望高手赐教!
...全文
255 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenhui9876 2004-07-30
  • 打赏
  • 举报
回复
自己知道还问,那就结贴
99666 2004-07-30
  • 打赏
  • 举报
回复
正像钻石里的泡泡说的那样,不是关键字的问题。form中的id不能和函数名称相同。好了,明天结贴。
stefli 2004-07-30
  • 打赏
  • 举报
回复
id=add
两个button使用同一个id是不允许的.
<html>
<head>
<script language="javascript">
function add(){
alert("add");
}
</script>
</head>
<body>
<form>
<input type="button" id="add1" value="Add" onClick="add()" />
<input type="button" id="add2" value="Add" onClick="add()" />
</form>
</body>
</html>
BubbleInDiamond 2004-07-30
  • 打赏
  • 举报
回复
我试了,就是form中ID号与函数名一样导致的问题,form外的没关系
morris_lz 2004-07-30
  • 打赏
  • 举报
回复
<script language="javascript">
function add(){
alert("add");
}
</script>

<body>
<form>
<input type="button" id="add1" value="Add" onClick="add()" />
</form>
<input type="button" id="add2" value="Add" onClick="add()" />





input对象不能当数组对象用
BubbleInDiamond 2004-07-30
  • 打赏
  • 举报
回复
是不是id号和函数名一样导致的问题
99666 2004-07-30
  • 打赏
  • 举报
回复
嗨,天天被Javascript折磨,有没有哪个编辑工具有javascript属性和方法的提示呀,就像Dreamweaver对html有提示一样?
99666 2004-07-30
  • 打赏
  • 举报
回复
loku兄是不是说有两个button的id都是add呀?不是这个问题啊。
不信你把第二个button的id改为add1,第一个按钮还是错误。
loku 2004-07-30
  • 打赏
  • 举报
回复
我觉得,是你的“函数名称”跟button的名称冲突的缘故
99666 2004-07-30
  • 打赏
  • 举报
回复
就算add是IE中关键字,为什么第二个按钮能正确执行?
ecobin 2004-07-30
  • 打赏
  • 举报
回复
MSDN Home > MSDN Library > Web Development > HTML and Dynamic HTML > Reference > Methods
add Method Internet Development Index

--------------------------------------------------------------------------------

Adds an element to the areas, controlRange, or options collection.

Syntax

object.add(oElement [, iIndex])
Parameters

oElement Required. Object that specifies the element to add to the collection.
iIndex Optional. Integer that specifies the index position in the collection where the element is placed. If no value is given, the method places the element at the end of the collection.

Return Value

No return value.

Remarks

Before you can add an element to a collection, you must create it first by using the createElement method.

This method can be used to add elements only after the page loads.

If the method is applied inline, a run-time error occurs.

Example

This example uses the add method to insert an object into the options collection of a select object.

<SELECT ID="oSelect">
<OPTION VALUE="1">One</OPTION>
</SELECT>

<SCRIPT>
var oOption = document.createElement("OPTION");
oSelect.options.add(oOption);
oOption.innerText = "Two";
oOption.value = "2";
</SCRIPT>
Standards Information

This method is defined in World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1 .

Applies To

[ Object Name ]
Platform Version
Win16:
Win32:
Windows CE:
Unix:
Mac:
Version data is listed when the mouse hovers over a link, or the link has focus.
areas, controlRange, options, SELECT
Move the mouse pointer over an element in the Applies To list to display availability information for the listed platforms.

See Also

remove
99666 2004-07-30
  • 打赏
  • 举报
回复
chinchy兄的资料很好,但是也没有add这个保留字呀!
而且第二个按钮也工作正常呀!
chinchy 2004-07-30
  • 打赏
  • 举报
回复
JScript 有许多保留字,它们在 JScript 语言语法中有特定的意义。因此,这些字不宜作为脚本中的函数、变量或常数的名称。总共有三类保留字。

受保护的保留字
break case catch class const
continue debugger default delete do
else export extends false finally
for function if import in
instanceof new null protected return
super switch this throw true
try typeOf var while with

受保护的保留字不能用作标识符。将受保护的保留字用作标识符会在加载脚本时引起编译错误。

注意 尽管“export”是一个受保护的保留字,但它没有实现方法。
新保留字
abstract boolean byte char decimal
double enum final float get
implements int interface internal long
package private protected public sbyte
set short static uint ulong
ushort void

JScript 还有一系列新保留字。像受保护的保留字一样,这些关键字在当前版本的 JScript 内有着特殊的意义。由于向后兼容的原因,新保留字可用作标识符。一旦将新保留字用作标识符,它就失去了作为脚本中关键字的意义。将新保留字用作标识符会引起混淆,应予以避免。

未来保留字
assert ensure event goto invariant
namespace native require synchronized throws
transient use volatile

JScript 有一系列未来保留字,这些保留字将被建议用作 JScript 的未来扩展中的关键字。像新保留字一样,这些保留字也可以在当前版本的 JScript 中用作标识符。然而,若避免使用这些字,则在更新脚本以利用未来版本的 JScript 中的功能时会更为方便。

在选择标识符时,避免选择已经是内部 JScript 对象或函数的名称(比如 String 或 parseInt)也是非常重要的
99666 2004-07-30
  • 打赏
  • 举报
回复
先顶一下,我在线等。

87,922

社区成员

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

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