我有下面的脚本代码
(function($) {
$.fn.youamaAjaxLogin = function(options) {
function start() {
//start
}
....
如何在外部调用函数start()
(function($) {
请帮忙
最佳答案
假设 start()
使用也在其父匿名函数中定义的变量/函数,我能想到的最简单的方法是在 jQuery 之外创建一个您分配的变量。然后您可以在 jQuery 内部和外部调用它。
注意:这样做的缺点是您只能在页面加载完成后调用 start();
。如果您尝试在此之前调用 start();
,将导致控制台错误。
这是一个例子...
var start = null;
$(function(){
var exampleVariable = "hello world";
start = function() {
alert(exampleVariable);
}
// You can call the function as normal here...
// start();
});
// You cannot call it here, because the page won't have loaded, so start won't be defined
// start();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" onclick="start();" value="Call Function"/>
关于javascript - 如何在插件脚本之外调用 jquery 插件函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29985991/