Javascript jQuery类选择器-访问选定元素上的属性?

Javascript jQuery类选择器-访问选定元素上的属性?,javascript,jquery,Javascript,Jquery,我目前有一个页面,它有多个元素,类为“chained”。我想使用一个类选择器,这样我就可以选择所有这些,并基于一些数据属性运行一些额外的代码(每个元素的属性都不同)。到目前为止,我的代码是: $('.chained_child').remoteChainedTo('chained_parent' + $(this).data('chained-parent'), $(this).data('chained-url')); 我认为问题出在这部分:$(this).data('chained-

我目前有一个页面,它有多个元素,类为“chained”。我想使用一个类选择器,这样我就可以选择所有这些,并基于一些数据属性运行一些额外的代码(每个元素的属性都不同)。到目前为止,我的代码是:

$('.chained_child').remoteChainedTo('chained_parent'
   + $(this).data('chained-parent'), $(this).data('chained-url'));
我认为问题出在这部分:
$(this).data('chained-parent')
-这似乎不起作用。如何选择所选元素的数据属性

谢谢

您可以使用该方法迭代匹配的元素集,并为每个元素运行一个函数:

$('.chained_child').each(function () {
    $(this).remoteChainedTo('chained_parent' + $(this).data('chained-parent'), $(this).data('chained-url'));
});
正如您在上面的代码中所看到的,在
.each()
回调
中,此
引用当前的底层DOM节点(不是jQuery对象,因此需要将其传递给jQuery)

请注意,一些jQuery方法将函数作为单个参数接受,在隐式
.each()
循环中,将为匹配集中的每个元素调用一次函数。查看您正在使用的插件的源代码可以看出,这里的情况并非如此。

您可以使用该方法迭代匹配的元素集,并为每个元素运行一个函数:

$('.chained_child').each(function () {
    $(this).remoteChainedTo('chained_parent' + $(this).data('chained-parent'), $(this).data('chained-url'));
});
正如您在上面的代码中所看到的,在
.each()
回调
中,此
引用当前的底层DOM节点(不是jQuery对象,因此需要将其传递给jQuery)

请注意,一些jQuery方法将函数作为单个参数接受,在隐式
.each()
循环中,将为匹配集中的每个元素调用一次函数。查看您正在使用的插件的源代码可以看出,这里的情况并非如此。

尝试使用

$(this).attr('data-chained-parent')

试用

$(this).attr('data-chained-parent')


我想你应该这样用

$(".chained").each(function(){
//add data as you want
})

我想你应该这样用

$(".chained").each(function(){
//add data as you want
})
什么是
$(此)。数据('chained-parent')
?如上所述-$(此)没有您放置它的上下文这可能是有趣的什么是
$(此)。数据('chained-parent')
?如上所述-$(此)没有您放置它的上下文这可能是有趣的