我正在使用jQuery Quicksand插件。我需要得到点击项目的数据id,并将其传递给一个webservice。

如何获得data-id属性?我正在使用.on()方法重新绑定排序项的单击事件。

$("#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>


当前回答

如果我们想使用现有的原生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');

阅读本文档

其他回答

你还可以使用:

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

如果你不担心旧的ie浏览器,你也可以使用HTML5数据集API。

HTML

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

JavaScript

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

Try

this.dataset.id

$(“#list li”).on('click', function() { 警报( this.dataset.id ); }); <script src=“https://code.jquery.com/jquery-3.3.1.slim.min.js”></script> <ul id=“list” class=“grid”> < 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>

使用jQuery:

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

如果我们想使用现有的原生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');

阅读本文档