English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery Miscellaneous data() Method

Métodos Diversos do jQuery

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.

Syntax:

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)

Example

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‹/›

Valor do Parâmetro

ParâmetroDescrição
keyEspecificar a chave (nome) dos dados a serem definidos
valueEspecificar o valor de dados a ser definido
objectO objeto de dados de chave/valor deve ser atualizado

Métodos Diversos do jQuery