简体   繁体   English

访问HTML数据集

[英]Access HTML dataset

I am trying to access a dataset in my HTML. 我正在尝试访问HTML中的数据集。 Normally if it was: 通常是:

<td data-mmyyyy="23"></td>

You could access it with 您可以使用

tdObj.dataset.mmyyyy

but for some reason that is not working for me here. 但由于某种原因在这里对我不起作用。

var mmyyyy = tds[i].dataset.mmyyyy.split('/');

I stopped it on the debugger and output the following from the console: 我在调试器上将其停止,并从控制台输出以下内容:

tds // (ME)
  [(enumerated td nodes)] // (CONSOLE)
tds[i] // (ME)
  <td> // (CONSOLE)
    <a href="#" data-mmyyyy="3/2015">22</a>
  </td>
tds[i].dataset // (ME)
  DOMStringMap {}  // (CONSOLE)
tds[i].dataset.mmyyyy // (ME)
  undefined  // (CONSOLE)

Can anyone tell me how to approach this differently to access that data attribute? 谁能告诉我如何以不同的方式来访问该数据属性? Thanks. 谢谢。

My mistake! 我的错! The dataset is on the tag within the tag. 数据集位于标签内的标签上。 So the correct accessor will be 因此正确的访问器将是

tds[i].children[0].dataset.mmyyyy

If you look better at the console, you'll see that it logs this: 如果您在控制台上看起来更好,则会看到它记录了以下内容:

<td>
    <a href="#" data-mmyyyy="3/2015">22</a>
</td>

So, according to the data logged by the console, the dataset you're trying to access is not on your <td> element, but on its <a> child. 因此,根据控制台记录的dataset您尝试访问的dataset不在您的<td>元素上,而是在其<a>子元素上。

To access it you can do this: 要访问它,您可以执行以下操作:

tds[i].children[0].dataset.mmyyyy

Or, better, with querySelector : 或者,更好的是使用querySelector

tds[i].querySelector("a").dataset.mmyyy

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM