我制作了一个 include.js
文件,将所有常规函数放入其中。在此之前,函数位于文件本身中。
我从 Chrome 控制台收到一条错误,指出 changeColor()
未定义。
我不想让它变得困惑,所以我将我的一些帖子粘贴到了 PasteBin 上。
if(username.val() == '')
{
changeColor('#inputname');
exclamation('#inputname');
$.notify("U hebt geen gebruikersnaam opgegeven", "error");
return false;
}
所以 changeColor()
未定义,exclamation()
也是如此。现在是 Pastebin 部分:http://pastebin.com/5bbPuT4e
如您所见,我有一个 header.php
文件,其中包含整个网站所需的所有 JS 文件。在 include.js
文件中创建函数,并在 aanmelden.js
文件中调用函数。在我看来,我已经看不清了,所以我真的不知道我做错了什么。
最佳答案
删除include.js中的document.ready。这会创建作用域,因此 ChangeColor 和感叹号仅存在于包装函数内。 include.js 应该是:
function changeColor(elem) {
$(elem).css({
"border-radius": "5px",
"font-weight": "bolder",
"border": "1px solid red"
});
}
function exclamation(elem) {
$(elem).notify(
"-",
{ position:"right" }
);
}
注意 document.ready 不再存在。
有关 javascript 中函数作用域的一些信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope
关于javascript - 函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25191517/