1

我想将宽度和高度参数传递给 .live() 函数。它可以工作,但看不到(this)标签。很快,是否可以将值传递给 jquery 中的实时函数?

4

2 回答 2

1

这是不可能的,但在函数内部,您可以访问this实例,该实例将是触发事件的 html 元素:

    $("#element").live("click", function(){
        var width = $(this).width();
        var height = $(this).height();
    })

使用 on() 是一样的,因为正如@FAddel 所说,它现在是推荐的方式

    $("#element").on("click", function(){
        var width = $(this).width();
        var height = $(this).height();
    })
于 2012-04-24T13:44:08.897 回答
1
jQuery(document).on('.yourElement', 'event', function() {
    var height = 800;
    var width = 400;
    jQuery(this).height(height);
    jQuery(this).width(width);
});

或者,如果你想活下去:

jQuery('.yourElement').live('click', function(){
    var height = 800;
    var width = 400;
    jQuery(this).height(height);
    jQuery(this).width(width);
});

如果你不想使用我推荐.delegate()

jQuery(document).delegate('.yourElement', 'click', function(){
    var height = 800;
    var width = 400;
    jQuery(this).height(height);
    jQuery(this).width(width);
});
于 2012-04-24T13:47:44.560 回答