如何获取data-id属性?

时间:2011-03-15 09:44:31

标签: javascript jquery custom-data-attribute

我正在使用jQuery quicksand插件。我需要获取所单击项的data-id并将其传递给webservice。 如何获取data-id属性?我正在使用.on()方法重新绑定已排序项的click事件。

$("#list li").on('click', function() {
  //  ret = DetailsView.GetProject($(this).attr("#data-id"), OnComplete, OnTimeOut, OnError);
  alert($(this).attr("#data-id"));
});
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>

<ul id="list" class="grid">
  <li data-id="id-40" class="win">
    <a id="ctl00_cphBody_ListView1_ctrl0_SelectButton" class="project" href="#">
      <img src="themes/clean/images/win.jpg" class="project-image" alt="get data-id" />
    </a>
  </li>
</ul>

15 个答案:

答案 0 :(得分:1428)

要获取属性data-id的内容(如<a data-id="123">link</a>中所示),您必须使用

$(this).attr("data-id") // will return the string "123"

.data()(如果您使用较新的jQuery&gt; = 1.4.3)

$(this).data("id") // will return the number 123

data-之后的部分必须为小写,例如data-idNum无效,但data-idnum将会有效。

答案 1 :(得分:148)

如果我们想使用现有的原生 JavaScript 检索或更新这些属性,那么我们可以使用getAttribute和setAttribute方法执行此操作,如下所示:

通过JavaScript

<div id='strawberry-plant' data-fruit='12'></div>

<script>
// 'Getting' data-attributes using getAttribute
var plant = document.getElementById('strawberry-plant');
var fruitCount = plant.getAttribute('data-fruit'); // fruitCount = '12'

// 'Setting' data-attributes using setAttribute
plant.setAttribute('data-fruit','7'); // Pesky birds
</script>

通过jQuery

// Fetching data
var fruitCount = $(this).data('fruit');
OR 
// If you updated the value, you will need to use below code to fetch new value 
// otherwise above gives the old value which is intially set.
// And also above does not work in ***Firefox***, so use below code to fetch value
var fruitCount = $(this).attr('data-fruit');

// Assigning data
$(this).attr('data-fruit','7');

Read this documentation

答案 2 :(得分:76)

重要提示。请注意,如果您通过JavaScript动态调整data- 属性,则不会反映在data() jQuery函数中。您还必须通过data()函数进行调整。

<a data-id="123">link</a>

JS:

$(this).data("id") // returns 123
$(this).attr("data-id", "321"); //change the attribute
$(this).data("id") // STILL returns 123!!!
$(this).data("id", "321")
$(this).data("id") // NOW we have 321

答案 3 :(得分:45)

如果您不关心旧版IE浏览器,也可以使用HTML5 dataset API

<强> HTML

<div id="my-div" data-info="some info here" data-other-info="more info here">My Awesome Div</div>

<强> JS

var myDiv = document.querySelector('#my-div');

myDiv.dataset.info // "some info here"
myDiv.dataset.otherInfo // "more info here"

演示:http://html5demos.com/dataset

完整浏览器支持列表:http://caniuse.com/#feat=dataset

答案 4 :(得分:12)

惊讶没人提到:

<select id="selectVehicle">
     <option value="1" data-year="2011">Mazda</option>
     <option value="2" data-year="2015">Honda</option>
     <option value="3" data-year="2008">Mercedes</option>
     <option value="4" data-year="2005">Toyota</option>
    </select>

$("#selectVehicle").change(function () {
     alert($(this).find(':selected').data("year"));
});

以下是工作示例:https://jsfiddle.net/ed5axgvk/1/

答案 5 :(得分:10)

我使用$ .data - http://api.jquery.com/jquery.data/

//Set value 7 to data-id 
$.data(this, 'id', 7);

//Get value from data-id
alert( $(this).data("id") ); // => outputs 7

答案 6 :(得分:10)

<强> HTML

<span id="spanTest" data-value="50">test</span>

<强> JS

$(this).data().value;

$("span#spanTest").data().value;
  

ANS:50

适合我!

答案 7 :(得分:9)

var id = $(this).dataset.id

适合我!

答案 8 :(得分:7)

使用自己的id访问数据属性对我来说有点容易。

$("#Id").data("attribute");

&#13;
&#13;
function myFunction(){
  alert($("#button1").data("sample-id"));
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button type="button" id="button1" data-sample-id="gotcha!" onclick="myFunction()"> Clickhere </button>
&#13;
&#13;
&#13;

答案 9 :(得分:4)

这段代码将返回数据属性的值,例如:data-id,data-time,data-name等..,我已经为id显示了

the_list[[i]]$function$time %>% max() 

JS:

<a href="#" id="click-demo" data-id="a1">Click</a>

//获取data-id的值 - &gt; A1

$(this).data("id");

//这将更改data-id - &gt; A2

$(this).data("id", "a2");

//获取data-id的值 - &gt; A2

答案 10 :(得分:3)

使用jQuery:

  $( ".myClass" ).load(function() {
    var myId = $(this).data("id");
    $('.myClass').attr('id', myId);
  });

答案 11 :(得分:1)

问题是您未指定下拉列表或列表的选项或选定选项,这是下拉列表的示例,我假设数据属性为data-record。

$('#select').on('change', function(){
        let element = $("#visiabletoID");
        element.val($(this).find(':selected').data('record'));
    });

答案 12 :(得分:1)

尝试

this.dataset.id

$("#list li").on('click', function() {
  alert( this.dataset.id );
});
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>

<ul id="list" class="grid">
  <li data-id="id-40" class="win">
    <a id="ctl00_cphBody_ListView1_ctrl0_SelectButton" class="project" href="#">
      <img src="themes/clean/images/win.jpg" class="project-image" alt="get data-id >>CLICK ME<<" />
    </a>
  </li>
</ul>

答案 13 :(得分:0)

对于希望动态删除并重新启用工具提示的用户,可以使用disposeenable方法。参见https://getbootstrap.com/docs/4.0/components/tooltips/#tooltipdispose

答案 14 :(得分:0)

我有跨度。我要使用已定义的data-txt-lang属性值。

Table Fields are :
id, duration, fromType, fromUser, recordingUrl, callLog, user, createdDate, fromNumber
相关问题