使用jQuery's.prop方法来设置函数的属性

Using jQuery's .prop method to set a property to a function

本文关键字:设置 函数 属性 方法 prop jQuery 使用      更新时间:2023-09-26

我正试图使用.prop() 将函数分配给一组元素上的属性

$('.whatever').prop({MyProp:MyFunc});

仔细看这个函数的文档,似乎有一种特殊的行为,即值是一个函数,并且属性不会被分配函数,而是函数返回值。

有没有一个简单的解决方法,或者我应该只使用

$('.whatever').each(function(id, item){item.MyProp=MyFunc;});

.prop()并非用于此目的。这就是为什么要使用.data()方法。

$('.whatever').data('MyFunc', MyFunc);
// somewhere else:
var MyFunc = $('.whatever').data('MyFunc');
MyFunc(args);