如何从 Javascript 执行 YUI 函数?

发布于 2024-09-27 02:23:55 字数 397 浏览 4 评论 0原文

我如何从 javascript 调用包装在 YUI().use 内的 YUI 函数?

例子 下面是一个 YUI 函数“runShowAnim”,它执行 animShow.run();对于动画效果...

 var runShowAnim = function(e) {
     animShow.run();
 };

我希望当我验证 javascript 函数中的某些内容时发生这种效果。我尝试如下调用它。但这似乎不起作用。

function notifyUser(message) {
   document.getElementById("msgArea").innerHTML = message;
   runShowAnim();
}

How can i call a YUI function that is wrapped inside a YUI().use from javascript?

example
Below is a YUI function "runShowAnim" which executes animShow.run(); for an animation effect...

 var runShowAnim = function(e) {
     animShow.run();
 };

I want this effect to happen when i validate something in a javascript function. I tried to call it as below. But it doesn't seem to work.

function notifyUser(message) {
   document.getElementById("msgArea").innerHTML = message;
   runShowAnim();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

趁年轻赶紧闹 2024-10-04 02:23:55

我通过将 YUI 函数完全夹在一个函数中并调用该函数来实现这一点。

var runShowAnim = function() { 
    YUI().use('anim', 'node', function(Y) {
        var animShow = new Y.Anim({
        node: '#msgArea',
        to: { height: 50,opacity:1 }
        });
        animShow.run();
    });
}; 

现在我可以调用 runShowAnim 而不会出现任何问题,如下面的示例函数所示。

function notifyUser(message) { 
   document.getElementById("msgArea").innerHTML = message; 
   runShowAnim(); 
} 

I achieved this by sandwiching the YUI function completely inside a function and calling that function..

var runShowAnim = function() { 
    YUI().use('anim', 'node', function(Y) {
        var animShow = new Y.Anim({
        node: '#msgArea',
        to: { height: 50,opacity:1 }
        });
        animShow.run();
    });
}; 

now i can call runShowAnim without any problem like in the below sample function..

function notifyUser(message) { 
   document.getElementById("msgArea").innerHTML = message; 
   runShowAnim(); 
} 
财迷小姐 2024-10-04 02:23:55

如果要调用函数,则必须在函数名称后添加 () 后缀,并在函数名称之间包含 0 个或多个以逗号分隔的参数。

runShowAnim();

如果该函数没有全局作用域(如果它是在传递给 use() 的函数内部声明的,则您的函数将具有全局作用域)并且没有以某种方式传递到外部,那么您只能从相同的位置执行此操作范围。

If you want to call a function, you have to suffix the function name with () and include 0 or more comma separated arguments between them.

runShowAnim();

If the function doesn't have global scope (as yours will have if it is declared inside a function passed to use()) and not passed outside in some way then you can only do this from the same scope.

叫嚣ゝ 2024-10-04 02:23:55

我认为你漏掉了括号。

function notifyUser(message) {
   document.getElementById("msgArea").innerHTML = message;
   runShowAnim(); // right here
}

I think you're missing the parentheses.

function notifyUser(message) {
   document.getElementById("msgArea").innerHTML = message;
   runShowAnim(); // right here
}
橘亓 2024-10-04 02:23:55

YUI.thefunction()?

我认为你也需要用命名空间来调用它,

类似于

var X = function(){};
X.Y = function(){};
X.Y.Z = function(){};
X.Y.Z.foo = function(e){alert(e);}

//foo("me");<-error

X.Y.Z.foo("me");

YUI.thefunction()?

I think you need to call it with namespace too

something similar to

var X = function(){};
X.Y = function(){};
X.Y.Z = function(){};
X.Y.Z.foo = function(e){alert(e);}

//foo("me");<-error

X.Y.Z.foo("me");
流心雨 2024-10-04 02:23:55

如果要从闭包外部调用已在闭包内部定义的函数(作为最后一个参数传递给 YUI.use 的函数),则需要全局公开该函数。

要么在闭包外部定义一个全局变量并将您的函数分配给它,要么将您的函数分配给窗口对象,

var runShowAnim;

YUI().use(function(e){
  runShowAnim = function(){alert('called');}
});

runShowAnim();

If you want to call a function that has been defined inside the closure (the function passed as the last parameter to YUI.use) from outside it, you need to expose the function globally.

Either define a global variable outside the closure and assign your function to it, or assign your function to the window object

i.e.

var runShowAnim;

YUI().use(function(e){
  runShowAnim = function(){alert('called');}
});

runShowAnim();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文