JavaScript函数导致所有其他函数不能在js文件中工作 [英] JavaScript function causing all other functions not to work inside js file

查看:120
本文介绍了JavaScript函数导致所有其他函数不能在js文件中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Safari 4和所有浏览器浏览器中,每当我尝试在包含以下函数的JavaScript文件中调用函数时,都不会调用第一个函数。



所以调用function1不会工作,如果function2是在同一个.js文件,解释?



这是造成问题的代码。每当我删除这个功能,一切工作正常,所有功能都正常工作。所以这个函数会引发一个问题。
$ b $ pre $ function $ add $ option
{
var optn = document.createElement(OPTION);
optn.text = text;
optn.value = value;
optn.id = value;
if(class == 1){optn.className =nav_option_main; }
selectbox.options.add(optn);
}

任何想法为何?

感谢

解决方案

可能会因为您使用保留字( class )作为变量标识符。尝试使用klazz或其他东西。

这是一个保留字列表的链接: en.wikibooks.org/wiki/JavaScript/Reserved_Words

这里是Monsieur Crockford的一些单词(向下滚动或搜索短语 JavaScript非常笨手在对保留字的限制中): crockford.com :)

In safari 4 and all explorer browsers, whenever I try to call a function inside a javascript file which contains this function below, that first function isn't called.

So calling function1 will not work if function2 is inside the same .js file, explanation?

Here is the code which makes the problem. Whenever I remove this function, everything works fine and all functions work fine. So this function is causing a problem.

 function addOption(selectbox, value, text, class, id_nr )
 {
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
optn.id = value;
if (class==1){ optn.className = "nav_option_main"; }
selectbox.options.add(optn);
 }

Any ideas why?

Thanks

解决方案

Maybe it gets upset because you use a reserved word (class) as a variable identifier. Try using klazz or something else.

Here is a link to a list of reserved words: en.wikibooks.org/wiki/JavaScript/Reserved_Words
And here's some words from Monsieur Crockford (scroll down or search for the phrase "JavaScript is very heavy handed in its restrictions on reserved words"): crockford.com :)

这篇关于JavaScript函数导致所有其他函数不能在js文件中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆