How can I get a HTML data value as a string with jQuery?

后端 未结 7 825
面向向阳花
面向向阳花 2021-01-31 18:02

I have the following in my source HTML:

  • Test
  • I want to get the value of th

    7条回答
    •  梦毁少年i
      2021-01-31 18:27

      this.dataset.value;
      
      // Or old school
      this.getAttribute('data-value');
      

      const a = document.querySelector("a");
      console.log('Using getAttribute, old school: ', a.getAttribute('data-value'));
      console.log('Using dataset, conforms to data attributes: ', a.dataset.value);

      Thanks to @MaksymMelnyk for the heads up on dataset

    提交回复
    热议问题