“Dapatkan nilai atribut HTML di JS” Kode Jawaban

Dapatkan nilai atribut HTML di JS

//HTML
//<div id="elem" data-id="4hJ3s"></div>

var elem = document.getElementById("elem");
elem.getAttribute("data-id"); // "4hJ3s"
TC5550

Dom Element Get Atribut

document.getElementById('id1').getAttribute('attribute');
Jesus

JavaScript mendapatkan atribut

<div id="id_of_the_element" data-attribute_name="foo"></div>

var elem = document.getElementById("id_of_the_element");
var attribute_value = elem.getAttribute("data-attribute_name");

//OR with jquery...
var attribute_value = $('#id_of_the_element').attr('data-attribute_name');
//OR...
var attribute_value = $('#id_of_the_element').data('attribute_name');
Matteoweb

Dapatkan nilai atribut HTML di JS

<div id="id_of_the_element" data-attribute_name="foo"></div>

var elem = document.getElementById("id_of_the_element");
var attribute_value = elem.getAttribute("data-attribute_name");

//OR with jquery...
var attribute_value = $('#id_of_the_element').attr('data-attribute_name');
//OR...
var attribute_value = $('#id_of_the_element').data('attribute_name');

2nd example

//HTML
//<div id="elem" data-id="4hJ3s"></div>

var elem = document.getElementById("elem");
elem.getAttribute("data-id"); // "4hJ3s"
Clear Copperhead

Jawaban yang mirip dengan “Dapatkan nilai atribut HTML di JS”

Pertanyaan yang mirip dengan “Dapatkan nilai atribut HTML di JS”

Lebih banyak jawaban terkait untuk “Dapatkan nilai atribut HTML di JS” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya