[英]How can I access the function which inside different js file in JQUERY?
There is a script which named Normal.js in the HTML: HTML中有一个名为Normal.js的脚本:
$(function () {
function WhiteNav() {
$("#LOGO>img").attr('src', '/images/LOGOWhite.svg');
$("#NavUL a").css("color", "#cecece");
}
});
And here is the struct of the HTML as below: 这是HTML的结构,如下所示:
However, after the browser(Chrome) ran the WhiteNav function in the script, it reported this error and the WhiteNav failed: 但是,浏览器(Chrome)在脚本中运行WhiteNav函数后,它报告了此错误,并且WhiteNav失败:
Why it turned out to be this? 为什么原来是这个? It seems like I ran the code with a different file, is it right?
好像我用其他文件运行了代码,对吗? I tried the way as Why jQuery click doesn't work when included in a separate file said but failed again.
我尝试了一种方式,因为当包含在一个单独的文件中时 , 为什么jQuery单击不起作用,但是又失败了。
What's wrong with this? 这怎么了 How can I solve this problem?
我怎么解决这个问题? Would you please help me?
你能帮我吗? Thank you.
谢谢。
You're declaring WhiteNav
inside of a function() { ... }
, meaning it will only be accessible from within that function. 您是在
function() { ... }
内部声明WhiteNav
,这意味着只能从该函数内部对其进行访问。
Simply remove the $(function () { ... });
只需删除
$(function () { ... });
from around it, and it will be declared globally - accessible from anywhere after it's declaration. 从它周围开始,它将被全局声明-声明后可从任何地方访问。
As a side-note, you can remove $(function () { ... });
作为旁注,您可以删除
$(function () { ... });
from your internal <script>
snippet as well. 也可以从您的内部
<script>
代码段中获取。 $(function () { ... });
and $(document).ready(function() { ... });
和
$(document).ready(function() { ... });
are identical and so there is no need for both. 是相同的,因此两者都不需要。 The latter is just shorthand.
后者只是简写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.