jquery - 插件气泡点击事件

发布于 2024-10-05 22:18:12 字数 878 浏览 0 评论 0原文

我正在尝试创建自己的 jquery 插件,但在将事件冒泡到页面时遇到问题(我认为这就是问题所在)。

我这样调用插件。

$('div.testdiv1').bpcolourpicker
({
    onClick: onClickCallback
});

var onClickCallback = function ()
{
    //alert('testpage');
}

插件代码-

    var defaults =
    {
        returncolour: 'ff0000',
        showtextbox: true,
        onClick: function () { }
    };
    var options = $.extend(defaults, options);



    var onClick = function ()
    {
        // default
    }

    // Select a colour
    $(thisdiv).find('div.bpcolourpicker table.colours div').click(onClick, function ()
    {
        var divtitle = RGBToHex($(this).attr('title'));
        $(thisdiv).find('input#chosencolour').val(divtitle);
    });

所以我想要发生的是当点击方法被触发时,我想在调用“onClick:onClickCallback”时将该事件冒泡到主页,所以我认为 onClickCallBack 会处理该事件。

有人可以帮忙吗?

提前致谢。

i'm trying to create my own jquery plugin but am having problems bubbling the event to the page (I think this is the problem).

I am calling the plugin like so.

$('div.testdiv1').bpcolourpicker
({
    onClick: onClickCallback
});

var onClickCallback = function ()
{
    //alert('testpage');
}

PLUGIN CODE-

    var defaults =
    {
        returncolour: 'ff0000',
        showtextbox: true,
        onClick: function () { }
    };
    var options = $.extend(defaults, options);



    var onClick = function ()
    {
        // default
    }

    // Select a colour
    $(thisdiv).find('div.bpcolourpicker table.colours div').click(onClick, function ()
    {
        var divtitle = RGBToHex($(this).attr('title'));
        $(thisdiv).find('input#chosencolour').val(divtitle);
    });

So what I want to happen is when the click method is fired, I want to bubble that event to the main page as i'm calling 'onClick: onClickCallback' so I would have thought onClickCallBack would handle the event.

Can anyone help?

Thanks in advance.

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

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

发布评论

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

评论(1

圈圈圆圆圈圈 2024-10-12 22:18:12

您在设置变量之前传递onClickCallback

您的代码失败的方式与

alert(num);
var num = 3;

如果您将回调更改为函数语句相同,例如function onClickCallback() { ... },它将起作用。与普通变量不同,函数语句可以在定义之前使用。


此外,您无法通过向 .click() 传递两个参数来添加两个事件处理程序。
相反,您应该从处理程序中显式调用 onClick()

You're passing onClickCallback before you set the variable.

Your code is failing the same way as

alert(num);
var num = 3;

If you change the callback to a function statement, such asfunction onClickCallback() { ... }, it will work. Function statements are usable before their definitions, unlike normal variables.


Also, you cannot add two event handlers by passing two parameters to .click().
Instead, you should explicitly call onClick() from your handler.

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