English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The data() method stores any data in the selected element or retrieves data from it.
When using the data() methodObtaindata, it will returnthe first selected elementdata.
When the data() method is used forStoragedata, it will store the data inAll selected elements.
To delete data, useremoveData()Method.
Return the stored data of the selected element:
$(selector).data(key)
Store data in the selected element:
$(selector).data(key, value)
Use an object to store data in the selected element:
$(selector).data(key, object)
Retrieve data from the first list item:
$("#btn1").click(function(){ alert($("li").data("price")); });Teste e veja‹/›
Store data in a DIV element and then retrieve the data:
// Store data $("#btn1").click(function(){ $("div").data("msg", "Hello World"); }); // Get data $("#btn2").click(function(){ $("div").text($("div").data("msg")); });Teste e veja‹/›
Use an object to store data in a DIV element and then retrieve the data:
$("button").click(function(){ $("div").data("test", {first: 16, last: "pizza!"}); $("span:first").text($("div").data("test").first); $("span:last").text($("div").data("test").last); });Teste e veja‹/›
Procurar pelo atributo "data-video" através do botão:
$("#videoModal").on("show.bs.modal", function(event) { let button = $(event.relatedTarget); // Botão que dispara o modal let url = button.data("video"); // Dos dados-Extrair URL das propriedades de vídeo $(this).find("iframe").attr({ src: url, allow: "accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" }); });Teste e veja‹/›
Parâmetro | Descrição |
---|---|
key | Especificar a chave (nome) dos dados a serem definidos |
value | Especificar o valor de dados a ser definido |
object | O objeto de dados de chave/valor deve ser atualizado |