Jquery前端点击处理程序 [英] Jquery prepend click handler

查看:70
本文介绍了Jquery前端点击处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您知道更好的方法,请告诉我。
我有一个页面,其中将包含大量的按钮,图表和表。
我已经阅读了关于停止事件传播,这就是我将使用。
我想在cerain元素上启用帮助。我有一个复选框,改变光标在帮助启用div。我想做的是在正常点击行为之前添加帮助功能。



基本上是这样的。

 < input type =buttonvalue =点击我onclick =alert('hello')/> 

我想要的是,如果复选框被点击,我添加一个css类我在正常点击之前添加了点击处理程序。当单击该元素时,运行helpfunction并且不触发默认的按钮点击处理程序。当帮助复选框未选中时,我想删除帮助功能。
我意识到,我可以hardocde这个为每个元素,并检查helperfunction检查复选框是否被选中。



John

$

b $ b

解决方案

  $(:button b //你的按钮
var btn = $(this);

//原始点击处理程序
var clickhandler = btn.attr(onclick);
btn.attr(onclick,return false;);

//新的点击处理程序
btn.click(function(){

if $(#chkbox)。attr(checked)== true){

// TODO:显示帮助
// TODO:catch event

}
else {

clickhandler();

}
});
});


If you know a better way to do this then please let me know. Right I have a page which will contain lots of buttons, charts and tables. I have read about stopping event propogation and that's what I'll be using. I want to enable help on cerain elements. I have a checkbox which changes the cursor on help enabled divs. What I want to do is prepend a help function before the normal click behaviour.

Basically it's like this.

<input type="button" value="Click me" onclick="alert ('hello')" />

what i want is that if the checkbox is clicked I add a css class (already done) and then I add a click handler before the normal click. When the element is clicked then the helpfunction is run and the default button click handler is not fired. When the help checkbox is unchecked I want to remove the help function. I realise that I could hardocde this for each element and have a check in the helperfunction to see if the checkbox is checked. But I'd prefer a more generic way of doing it.

Thanks

John

解决方案

        $(":button").each(function() {

            // your button
            var btn = $(this); 

            // original click handler
            var clickhandler = btn.attr("onclick");
            btn.attr("onclick", "return false;");

            // new click handler
            btn.click(function() {

                if ($("#chkbox").attr("checked") == true) {

                    // TODO: show your help
                    // TODO: catch event

                }
                else {

                    clickhandler();

                }
            });
        });

这篇关于Jquery前端点击处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆