Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 没有定义函数_Javascript_Jquery_Include - Fatal编程技术网

Javascript 没有定义函数

Javascript 没有定义函数,javascript,jquery,include,Javascript,Jquery,Include,我制作了一个include.js文件,在其中我放入了所有常规函数。在此之前,函数都在文件本身中 我从Chrome控制台收到一个错误,说没有定义changeColor() 我不想把这里弄得一团糟,所以我把我的一些帖子贴在了PasteBin上 if(username.val() == '') { changeColor('#inputname'); exclamation('#inputname'); $.notify("U hebt geen gebruikersnaam

我制作了一个
include.js
文件,在其中我放入了所有常规函数。在此之前,函数都在文件本身中

我从Chrome控制台收到一个错误,说没有定义
changeColor()

我不想把这里弄得一团糟,所以我把我的一些帖子贴在了PasteBin上

if(username.val() == '')
{
    changeColor('#inputname');
    exclamation('#inputname');
    $.notify("U hebt geen gebruikersnaam opgegeven", "error");
    return false;
}
因此,
changeColor()
是未定义的,
感叹号()
也是如此。现在是Pastebin部分:


正如您所见,我有一个
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中函数作用域的一些信息:

此外,您可以扩展jQuery原型,以更“jQuery方式”使用这些方法:

这样,您就不必担心定义函数的范围


来源:

changeColor()
感叹号()
正在您的
文档中本地声明…ready
函数。它们不存在于该功能之外。虽然等待调用
文档..ready
中的函数很好,但您不一定要在其中定义函数。感谢您的回复和编辑我的帖子:-)。成功了:-)非常感谢!它成功了,我会在5分钟内接受你的回答:-)谢谢,我不知道有这样的功能。可以及时使用!:-)
$.fn.extend({

  changeColor: function() {    
    return this.each(function() {
      this.css({
        "border-radius": "5px",
        "font-weight":   "bolder",
        "border":        "1px solid red"
      });
    });
  },

  exclamation: function() {
    return this.each(function() {
      this.notify("-", {position:"right"});
    });
  }

});

// Use the newly created methods like this:
$('#inputname').changeColor();
$('#inputname').exclamation();