简体   繁体   English

如何在 JavaScript 中找到等效的 .data() (jQuery)?

[英]How do I find the .data() (jQuery) equivalent in JavaScript?

I have this code我有这个代码

 var info = $($(r).find(".BuyPriceBox")).find(".PurchaseButton").data();

r is a html page returned from an ajax GET call, which contains a button which I am trying to get the data from r 是从 ajax GET 调用返回的 html 页面,其中包含我试图从中获取数据的按钮

which I am trying to change into the JavaScript equivalent我正在尝试将其更改为等效的 JavaScript

I can't find anything that would resemble ".data()" in JavaScript?我在 JavaScript 中找不到任何类似于“.data()”的东西? Could anyone help me with this, and if kind enough help me translate it to JavaScript?任何人都可以帮我解决这个问题,如果好心帮我把它翻译成 JavaScript?

Using a fake get call:使用假get调用:

function getData(cb) {
  var html = [
    '<div><div class="BuyPriceBox">',
    '<button class="PurchaseButton" data-id="one">Click</button>',
    '</div></div>'
  ].join('');
  setTimeout(cb, 1000, html);
}

getData(function(html) {

  // create a new element and attach the html received
  var temp = document.createElement('div');
  temp.innerHTML = html;

  // Then use `querySelector` to grab the element
  // and get the dataset (an object)
  var button = temp.querySelector('.BuyPriceBox .PurchaseButton');
  var id = button.dataset.id; // one

});

DEMO演示

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

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