“Datenattribute” Code-Antworten

JS erhalten Datenattribut

var element = document.querySelector('.element');
var dataAttribute = element.getAttribute('data-name');
// replace "data-name" with your data attribute name

console.log(dataAttribute);
Eloquent Design

HTML JavaScript Datenattribut finden

//javascript get html data attribute
<button data-id="1" >Click</button>
<button data-id="2" >Click</button>
<button data-id="3" >Click</button>
const btns=document.querySelectorAll('button[data-id]');
[...btns].forEach(btn => console.log(btn.getAttribute('data-id')))
CuteKittyCat

Datenattribute

<!--data-* attributes allow us to store extra information on standard 
, semantic HTML elements.
-->
<article
  id="electric-cars"
  data-columns="3"
  data-index-number="12314"
  data-parent="cars">
...
</article>
<!-- Accessing via JS -->
<script>
  const article = document.querySelector('#electric-cars');
  article.dataset.columns // "3"
</script>
<!--css-->
<style>
  article::before {
  content: attr(data-parent);
 }
  article[data-columns='3'] {
  width: 400px;
}

</style>
Bug Killer

JavaScript erhalten Sie Datenattributwert

const article = document.querySelector('#electric-cars');
 
article.dataset.columns // "3"
article.dataset.indexNumber // "12314"
article.dataset.parent // "cars"
Curious Crossbill

Ähnliche Antworten wie “Datenattribute”

Fragen ähnlich wie “Datenattribute”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen