我正在使用我的.js文件。当我的函数mainPaginationClicked被调用时,我希望它也执行另一个函数rotateMessage。 rotateMessage在我的html文档的脚本标签中声明。有没有办法从我的.js文件调用这个函数?
I'm working in my .js file. When my function, mainPaginationClicked is called, I want it to also execute another function, rotateMessage. rotateMessage is declared in the script tags of my html document. Is there a way to call this function from my .js file?
推荐答案只要您在HTML中展开函数之后加载JavaScript文件(或者仅在调用声明),那么你声明的函数将在同一个JavaScript上下文中可用。实际的文件位置并不重要,只有内存中的命名空间。
So long as you load the JavaScript file after the function is delcared in the HTML (or only invoke the function after it is declared), then the function you declare will be available within the same JavaScript context. The actual file location doesn't matter, only the in-memory namespaces.
浏览器的默认行为是在加载JavaScript时使用脚本标记,因此您可以按照您希望它们执行的顺序安全地编写JavaScript资源,而不用担心外部文件需要加载(除非明确控制,否则它们全部汇集在一起在全局命名空间中)。
The default behavior in a browser is to block while loading JavaScript using script tags, so you can safely write the JavaScript resources in the order that you want them executed without worrying about the fact that the external file needs to be loaded (and unless explicitly controlled otherwise they all get pooled together in a global namespace).
更多推荐
从.js文件中调用html文件中声明的js函数
发布评论