获取父节点的属性

发布于 2025-01-01 15:02:16 字数 484 浏览 3 评论 0原文

我正在尝试使用

$(this).parentNode.attr('data-element')

which 应该在字符串中返回 0 - 5 但它不起作用。我在这样的函数中使用它

$('.someClass').each(function(){
    $(this).html(SomeFunction('SomeString', $(this).parentNode.attr('data-element')));
});

所有具有类“someClass”的元素都有一个parentNode

<li class="element" data-element: 1 (or any number from 0 to 5 (including))> </li>

,我不知道错误在哪里。我做错了什么?

——大卫

I am trying to use

$(this).parentNode.attr('data-element')

which should return 0 - 5 in string but it just won't work. I am using it in a function like this

$('.someClass').each(function(){
    $(this).html(SomeFunction('SomeString', $(this).parentNode.attr('data-element')));
});

All the elements with class 'someClass' have a parentNode

<li class="element" data-element: 1 (or any number from 0 to 5 (including))> </li>

and I have no idea where is the mistake. What am I doing wrong?

--David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陪你到最终 2025-01-08 15:02:16

您在同一行代码中混合了 jQuery 和纯 JavaScript,这是行不通的。您可以使用:

$(this).parent().attr('data-element');   // jQuery

this.parentNode.getAttribute("data-element");   // plain javascript

parentNode 不是 jQuery 对象的属性,因此您不能按照您的方式混合使用这两者。获取父级的 jQuery 方法是 .parent()

You are mixing jQuery and plain javascript in the same line of code and that will not work. You can either use:

$(this).parent().attr('data-element');   // jQuery

or

this.parentNode.getAttribute("data-element");   // plain javascript

parentNode is not a property of a jQuery object, so you can't mix the two the way you were doing it. The jQuery method for getting the parent is .parent().

勿忘初心 2025-01-08 15:02:16

你应该这样做

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

,因为你不能在非 jQuery 对象上调用 attr()

You should do

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

because you can't call attr() on a non jQuery object

凉月流沐 2025-01-08 15:02:16

尝试这样做:

$(this).parent().attr('data-element');

有关 .parent() 等函数的更多信息,请参阅 JQuery 文档的遍历部分:
http://api.jquery.com/category/traversing/

Try doing this instead:

$(this).parent().attr('data-element');

For more information on functions like .parent() see the Traversing section of the JQuery documentation:
http://api.jquery.com/category/traversing/

萌︼了一个春 2025-01-08 15:02:16

使用 jquery 应该是:

$(this).parent().attr('data-element');

不使用 jquery 这将是:

this.parentNode.getAttribute("data-element")

Using jquery it should be:

$(this).parent().attr('data-element');

Without using jquery this would be:

this.parentNode.getAttribute("data-element")
一个人的旅程 2025-01-08 15:02:16

我更喜欢使用:

var item = $(this);

var parent = item.closest(".element"); //using the class for selection

//and then use parent.attr('data-element')

I prefer to use:

var item = $(this);

var parent = item.closest(".element"); //using the class for selection

//and then use parent.attr('data-element')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文