jquery 插件中的数据属性查找父项
jquery data attributes in plugin find parent
拥有这个简单的 jquery 插件,如果给定的数据属性存在于具有类名 "async" 的元素上,则通过 ajax 加载数据 我尝试确定数据应该在哪里包装但没有任何结果
(function($) {
$.fn.asyncLoader = function(options) {
var $self = this;
var settings = $.extend({}, options);
var attributes = [];
return this.each(function(i) {
attributes = $(this).data();
$.each(attributes, function(key, value) {
if (key == 'load') {
if (value.target) {
var el = $('.async').filter('[data-load = \'{"target" : "' + value.target + '" }\' ]');
el.load(value.target)
}
}
});
});
};
}(jQuery));
标记看起来像
<div class="async" data-load = '{"target": "/my-target"}'></div>
如何找到父元素?
改用回调 filter()
,
var el = $('.async').filter(function(){
return $(this).data('load') === '{"target": "'+ value.target +'"}'
});
或以下 - *这可能需要对对象进行额外检查。
var el = $('.async').filter(function(){
return $(this).data('load').target == value.target
});
拥有这个简单的 jquery 插件,如果给定的数据属性存在于具有类名 "async" 的元素上,则通过 ajax 加载数据 我尝试确定数据应该在哪里包装但没有任何结果
(function($) {
$.fn.asyncLoader = function(options) {
var $self = this;
var settings = $.extend({}, options);
var attributes = [];
return this.each(function(i) {
attributes = $(this).data();
$.each(attributes, function(key, value) {
if (key == 'load') {
if (value.target) {
var el = $('.async').filter('[data-load = \'{"target" : "' + value.target + '" }\' ]');
el.load(value.target)
}
}
});
});
};
}(jQuery));
标记看起来像
<div class="async" data-load = '{"target": "/my-target"}'></div>
如何找到父元素?
改用回调 filter()
,
var el = $('.async').filter(function(){
return $(this).data('load') === '{"target": "'+ value.target +'"}'
});
或以下 - *这可能需要对对象进行额外检查。
var el = $('.async').filter(function(){
return $(this).data('load').target == value.target
});