5

私は次のものを持っています

 var $header = $('.inner th');
 var $fixedHeader = $(".header1 th");

    $header.each(function (index) {
          // i need to copy all events from $header[index] to $fixedHeader[index]
    });

イベント ハンドラー (onClick、onDblClick など) を最初のセットの要素から 2 番目のセットの隣接する要素にコピーするにはどうすればよいですか?? 私はjqueryの初心者で、苦労しています。

4

4 に答える 4

5

I know this has already been answered a few times, but here is an all inclusive solution, based on the previous solutions.

function copyEvents(source, destination) {
    var events;

    //copying from one to one or more
    source = $(source).first();
    destination = $(destination);

    //get all the events from the source
    events = $(source).data('events');
    if (!events) return;

    //make copies of all events for each destination
    destination.each(function() {
        var t = $(this);
        $.each(events, function(index, event) {
            $.each(event, function(i, v) {
                t.bind(v.type, v.handler);
            });
        });
    });
}
于 2012-08-02T15:16:57.407 に答える
2

jquery の.data('events')メソッドを使用する必要があります。

どこから始めればよいかを理解するために、動作する jsFiddle を用意しました。

http://jsfiddle.net/hx8gf/2/

Alphamale が私を打ち負かしたことは知っていますが、参考になれば投稿しておきます。

とにかくほとんど同じです...

于 2012-04-19T05:51:00.020 に答える
2

このようなものが役立つかもしれません:

var header = $('.inner th');
var fixedHeader = $(".header1 th");

header.each(function (index) {

    var events = $(header).data("events"); //Gives you all events of an element

    $.each(events, function(i, event) { //Loop through all the events

     $.each(event, function(j, h) { //Loop through all the handlers attached for a event

      fixedHeader.bind(i, h.handler); //Bind the handler with the event 

     });

    });

});

お役に立てれば。

于 2012-04-19T05:41:14.787 に答える
0

jqueryのclone(true)関数はイベントハンドリングをコピーできず、要素しかコピーできません。コピーしたい場合は、イベントバインディングを追加しようとしています。JQUERYのイベントバインディングを見ることができます。コールバック関数でバインド関数を呼び出します。これは役立つかもしれません。

于 2012-04-19T05:51:30.387 に答える