4

大家好,我有一个有趣的问题

是否可以复制这样的元素的 onclick 事件

$('#ElementId').attr('OldOnClick',$('#ElementId').attr('OnClick'));

如果有任何方法可以做到这一点,请指导我。

我正在尝试禁用表单上存在的所有元素的点击事件,在某些时候我必须恢复它们,所以我试图onclick用其他属性名称保存它们

4

6 回答 6

5

是的,您可以访问处理程序函数。在jquery中这样做的方式是这样的:

$('#specialbutton').click(function(){ console.log("peaches");};

var handlerFunction = $('#specialbutton').data("events").click[0].handler;

$('#specialbutton').click(); // "peaches"
handlerFunction(); // "peaches"

然后,您可以将其分配给另一个元素,如下所示:

$('#otherelement').click(handlerFunction);
$('#otherelement').click(); // "peaches"
于 2012-10-20T12:09:46.263 回答
3

我编写了用于复制事件的 jQuery 插件。虽然,它仅适用于动态添加的事件,但不适用于添加“on”功能的事件。

我希望这会对某人有所帮助。

参数:

eventType - “点击”、“鼠标悬停”等。

目的地 - jQuery 对象、dom 元素或选择器

clearCurrent - 如果为 true,它将清除目标处的当前处理程序 - 默认为 false

    (function($){
            $.fn.copyEventTo = function(eventType, destination, clearCurrent) {
            var events = [];
            this.each(function(){
                var allEvents = jQuery._data(this, "events");
                if (typeof allEvents === "object") {
                    var thoseEvents = allEvents[eventType];
                    if (typeof thoseEvents === "object") {
                        for (var i = 0; i<thoseEvents.length; i++){
                            events.push(allEvents[eventType][i].handler);
                        }           
                    }
                }
            });
            if (typeof destination === "string") {
                destination = $(destination);
            } else if (typeof destination === "object") {
                if (typeof destination.tagName === "string") {
                    destination = $(destination);
                }
            }
            if (clearCurrent === true) destination.off(eventType);
            destination.each(function(){
                for(var i = 0; i<events.length; i++) {
                    destination.bind(eventType, events[i]); 
                }
            });
            return this;
        }
    })(jQuery);
于 2013-07-11T10:13:45.667 回答
1

这里有另一种方式:

//sw Get Element
var button = document.getElementById("mybutton");
//sw Get Click Function
var clickFunction = jQuery._data(button, "events").click[0].handler;
//sw Test Click Function
clickFunction();
//sw Copy ClickFunction to another Element
$('#anotherButton').click(clickFunction);

注意:前面的代码适用于:

$("#mybutton").click(function() {
    //sw TODO HERE
});
于 2021-12-04T17:23:18.727 回答
0

听起来您可能错过了下面的第二行。当你在做副本时,你会想要做一个动作。

$('#ElementId').attr('oldonclick', $('#ElementId').attr('onclick') )
               .removeAttr('onclick');
于 2012-10-20T13:27:10.427 回答
0

以下将删除元素数据中的“onclick”存储,并允许您稍后从数据中使用它。A使用标签的示例

演示 http://jsfiddle.net/u4eVW/1/

/* remove "onclick" and store */
$('a').each(function() {
    var onclick = $(this).attr('onclick')
    $(this).data('oldClick', onclick)
    $(this).removeAttr('onclick')

})

/* initialize handlers from data */
$('a').click(function() {
        eval($(this).data('oldClick'))
 }) 

如果您希望更改单个“onclick”中的某些内容,则需要将属性值视为字符串并使用正则表达式方法进行修改。

<a href="#" onclick="alert('foo')"> Link 1</a>

var onclick=$(selector).attr('onclick').replace('foo', 'bar')
于 2012-10-20T13:33:50.120 回答
0

显然你不应该再这样做了,

这就是你无论如何都可以做到的(感谢这个答案):

var handlerFunction = jQuery._data( $('#ElementId').get(0), "events" ).click[0].handler;

然后,就像ChuckE的回答一样:

$('#otherelement').click(handlerFunction);
$('#otherelement').click(); // "peaches"

不要告诉任何人...

于 2019-07-10T22:31:33.697 回答