简体   繁体   English

如何访问具有相同类名的元素的数据属性?

[英]How do I access data attribute of elements with the same class name?

<div class="range" data-max="100">0-100</li>
<div class="range" data-max="200">0-200</li>
<div class="range" data-max="300">0-300</li>

I wish to access data-max in my javascript. 我希望在我的JavaScript中访问data-max。 For example, if I click 0-200, I get 200 only. 例如,如果我单击0-200,则只会得到200。 Here is my javascript. 这是我的JavaScript。

const ranges = document.querySelectorAll(".range");
for(var i = 0; i < ranges.length; i++){
    ranges[i].addEventListener('click', function(){
        console.log(this.dataset.max);
    });
}

The output when I click 0-200 is 我单击0-200时的输出是

200 100 200 100

I'm expecting 200. 我希望是200。

Here are my questions: 这是我的问题:
1. how to get expected value---200 as in the example. 1.如示例所示,如何获得期望值--- 200。
2. What is the scope of this in this.dataset.max in my code? 2.我的代码中this.dataset.max的范围是什么? I thought its scope is the function. 我以为它的作用范围就是功能。
3. When I change this to ranges[i], it seems ranges[i] is undefined. 3.当我将其更改为range [i]时,似乎range [i]未定义。 why? 为什么?

guessNum.js:4 Uncaught TypeError: Cannot read property 'dataset' of undefined guessNum.js:4未捕获的TypeError:无法读取未定义的属性'dataset'

I'm expecting answers in pure javascript. 我期待纯JavaScript的答案。 No jquery pls. 没有jquery请。

Well your html is invalid, if you fix it it will work. 好吧,您的html无效,如果您对其进行修复,它将可以正常工作。
What is happening is the divs become nested inside each other and the click events bubble up the divs. 发生的情况是div相互嵌套,而click事件使div冒泡。

 const ranges = document.querySelectorAll(".range"); for(var i = 0; i < ranges.length; i++){ ranges[i].addEventListener('click', function(){ console.log(this.dataset.max); }); } 
 <div class="range" data-max="100">0-100</div> <div class="range" data-max="200">0-200</div> <div class="range" data-max="300">0-300</div> 

Please avoid asking multiple questions, and please don't change the question to something entirely different. 请避免问多个问题,也不要将问题改成完全不同的问题。 The original should have been closed as it was a simple typo, and the additional questions are duplicates. 原来应该已经关闭,因为它只是一个简单的错字,其他问题都是重复的。

  1. What is the scope of this in this.dataset.max in my code? 我的代码中this.dataset.max的范围是什么? I thought its scope is the function. 我以为它的作用范围就是功能。

this is always scoped to the current execution context. 始终限于当前执行上下文。 However, it's set in a number of ways, mostly in the call. 但是,它有多种设置方式,主要是在通话中。 Arrow functions adopt the this of their outer context, which is arguably how it's set, not how it's scoped. 箭头函数采用其外部上下文的this ,可以说它是如何设置的,而不是它的范围。

Where a listener is added using addEventListener , then this within the function references the element on which the listener was placed. 使用addEventListener添加侦听器的地方, 函数内的引用引用放置侦听器的元素。 That isn't about scope, it's how this is set within the function. 这是不是范围,这是是怎么设定的功能中。

See How does the “this” keyword work? 请参阅“ this”关键字如何工作?

  1. When I change this to ranges[i], it seems ranges[i] is undefined. 当我将其更改为range [i]时,似乎range [i]是未定义的。 why? 为什么?

Because i within the listeners has a closure to the original i , which has been incremented to ranges.length and hence references a non-existent element of the collection. 因为听众内具有封闭到原来的 ,已递增到ranges.length并且因此引用该集合的不存在的单元。 See JavaScript closure inside loops – simple practical example . 请参阅循环内的JavaScript封闭-简单的实际示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当元素具有相同的类时,如何分配数据? - How do I assign data when elements have the same class? 如何在javascript中为具有相同类名的元素执行相同的功能? - How do I give elements with the same class name to perform the same function in javascript? 如何使用Javascript更改具有相同类名的多个元素? - How do I change multiple elements with same class name using Javascript? Angular 8/9 如何获得具有相同 class 名称的多个元素的 innerHTML? - Angular 8/9 How do I get the innerHTML of multiple elements with the same class name? 如何使用相同的数据填充页面上的多个元素(具有相同的类)? - How do I populate several elements on a page (that have the same class) with the same data? 如何仅访问具有相同 class 的特定元素? jquery - How do I access only specific elements that have a same class? jquery 如何为具有相同类名的多个元素设置动画? - How do you animate multiple elements with the same class name? 如何选择具有相同类名的所有元素? - How can I select all elements with the same class name? 使用数据属性和jQuery,如何通过数据属性名称和数据属性值选择元素 - Using data attributes and jQuery how can I select elements by data attribute name AND data attribute value 如何访问具有相同名称属性的img标签 - How to access img tag with the same name attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM