从.js文件中调用html文件中声明的js函数

编程入门 行业动态 更新时间:2024-10-08 13:33:31
本文介绍了从.js文件中调用html文件中声明的js函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用我的.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函数

本文发布于:2023-11-28 17:55:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   函数   声明   js   html

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!