雅虎 UI 自定义按钮

发布于 2024-08-16 07:06:10 字数 966 浏览 5 评论 0原文

我有一个通过 HTML 标记定义的 YUI 按钮。我设法正确地加载和“蒙皮”。

问题是自定义点击事件。我尝试了多种方法,所有这些方法都将自定义函数链接到“单击”事件,但无论我采用哪种方式,它总是在页面加载时触发,然后单击时不会触发。我似乎无法让它“等待”用户点击。在他第一次约会时,它就像处女一样。

代码如下....

<script type="text/javascript">
    YAHOO.util.Event.onContentReady("submitbutton", onButtonReadySubmit);
    YAHOO.util.Event.onContentReady("editbutton",onButtonReadyEdit);

    var myTabs = new YAHOO.widget.TabView("demo");
    function editDoc(sBaseRef, sUNID) {
        var sNewURL = sBaseRef + "/0/" + sUNID + "?EditDocument";
        alert("Going to : " + sNewURL);
     window.location.href=sNewURL;
    }
    function onButtonReadySubmit() {
 var oSubmitButton = new YAHOO.widget.Button("submitbutton");
    }
    function onButtonReadyEdit() {
        var oEditButton = new YAHOO.widget.Button("editbutton");
        YAHOO.util.Event.addListener("editbutton", 'click', editDoc('a URL path goes here' , 'A PageKey goes here'));
    }

I have a YUI button defined through HTML markup. I managed to get it loaded and "skinned" properly.

The problem is a custom click event. I have tried a number of approaches all of which links the custom function to the 'click' event, but no matter which way I do it, it ALWAYS triggers upon page loading and then it doesn't fire when clicked. I can't seem to get it to "wait" for a user to click. It just shoots like a virgin on his first date.

Code below....

<script type="text/javascript">
    YAHOO.util.Event.onContentReady("submitbutton", onButtonReadySubmit);
    YAHOO.util.Event.onContentReady("editbutton",onButtonReadyEdit);

    var myTabs = new YAHOO.widget.TabView("demo");
    function editDoc(sBaseRef, sUNID) {
        var sNewURL = sBaseRef + "/0/" + sUNID + "?EditDocument";
        alert("Going to : " + sNewURL);
     window.location.href=sNewURL;
    }
    function onButtonReadySubmit() {
 var oSubmitButton = new YAHOO.widget.Button("submitbutton");
    }
    function onButtonReadyEdit() {
        var oEditButton = new YAHOO.widget.Button("editbutton");
        YAHOO.util.Event.addListener("editbutton", 'click', editDoc('a URL path goes here' , 'A PageKey goes here'));
    }

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

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

发布评论

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

评论(2

绿光 2024-08-23 07:06:11

YUI Button 发布您在 YUI Button 实例上订阅的自己的单击事件,而不是使用 YAHOO.util.Event.addListener。 YUI Button 登陆页面对此进行了详细描述: http://developer.yahoo.com /yui/button/#handlingevents

YUI Button publishes its own click event that you subscribe to on the YUI Button instance, rather than using YAHOO.util.Event.addListener. This is described in detail on the YUI Button landing page: http://developer.yahoo.com/yui/button/#handlingevents.

你的呼吸 2024-08-23 07:06:11

你的问题是第三个参数。那应该是对函数的引用。您的代码中发生的情况是,第 3 个参数是一个函数,在创建侦听器时调用该函数,并且不返回任何内容。

您拥有:

YAHOO.util.Event.addListener("editbutton", 'click', editDoc(
    'a URL path goes here', 
    'A PageKey goes here'
));

您想要的只是:

YAHOO.util.Event.addListener("editbutton", 'click', editDoc);

但是,您还希望将“此处为 URL 路径”和“此处为 PageKey”传递给点击时的函数 -时间。为此,请使用 addListener() 的可选第四个参数 - 要传递给函数的对象。

function editDoc (ev, oArgs) {
   var sBaseRef = oArgs.url,
       sUNID    = oArgs.key;

   /* code here */

}

YAHOO.util.Event.addListener("editbutton", 'click', editDoc, { 
    url: 'a URL path goes here',
    key: 'A PageKey goes here'
});

请参阅 http://developer.yahoo.com/yui/docs /YAHOO.util.Event.html#method_addListener 了解更多。

正如 Todd 提到的,您也可以将其作为 YUI 按钮创建的一部分来执行 - 但同样存在函数与函数引用的问题。

Your problem is the 3rd argument. That should be a reference to function. What happens in your code is that the 3 argument is a function that is called as the listener is being created, and returns nothing.

You have:

YAHOO.util.Event.addListener("editbutton", 'click', editDoc(
    'a URL path goes here', 
    'A PageKey goes here'
));

Simply what you want is:

YAHOO.util.Event.addListener("editbutton", 'click', editDoc);

However, you also want to pass 'a URL path goes here' and 'A PageKey goes here' to the function at click-time. To do this, use the optional fourth argument to addListener() - an object to pass to the function.

function editDoc (ev, oArgs) {
   var sBaseRef = oArgs.url,
       sUNID    = oArgs.key;

   /* code here */

}

YAHOO.util.Event.addListener("editbutton", 'click', editDoc, { 
    url: 'a URL path goes here',
    key: 'A PageKey goes here'
});

See http://developer.yahoo.com/yui/docs/YAHOO.util.Event.html#method_addListener for more.

As Todd mentions, you can also do this as part of the YUI Button creation - but the same issues of function versus function references apply.

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