[英]How can I get the data-id attribute?
I'm using the jQuery Quicksand plugin.我正在使用 jQuery Quicksand插件。 I need to get the data-id of the clicked item and pass it to a webservice.
我需要获取单击项目的数据 ID 并将其传递给网络服务。
How do I get the data-id attribute?如何获取 data-id 属性? I'm using the
.on()
method to re-bind the click event for sorted items.我正在使用
.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>
To get the contents of the attribute data-id
(like in <a data-id="123">link</a>
) you have to use要获取属性
data-id
的内容(如在<a data-id="123">link</a>
),您必须使用
$(this).attr("data-id") // will return the string "123"
or .data()
(if you use newer jQuery >= 1.4.3)或
.data()
(如果您使用较新的 jQuery >= 1.4.3)
$(this).data("id") // will return the number 123
and the part after data-
must be lowercase, eg data-idNum
will not work, but data-idnum
will.而
data-
后面的部分必须是小写的,例如data-idNum
不会起作用,但是data-idnum
会起作用。
If we want to retrieve or update these attributes using existing, native JavaScript , then we can do so using the getAttribute and setAttribute methods as shown below:如果我们想使用现有的原生JavaScript检索或更新这些属性,那么我们可以使用 getAttribute 和 setAttribute 方法来实现,如下所示:
Through JavaScript通过 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>
Through jQuery通过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');
Important note.重要的提示。 Keep in mind, that if you adjust the
data-
attribute dynamically via JavaScript it will not be reflected in the data()
jQuery function.请记住,如果您通过 JavaScript 动态调整
data-
属性,它不会反映在data()
jQuery 函数中。 You have to adjust it via data()
function as well.您还必须通过
data()
函数对其进行调整。
<a data-id="123">link</a>
JavaScript: JavaScript:
$(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
If you are not concerned about old Internet Explorer browsers, you can also use HTML5 dataset API .如果你不关心旧的 Internet Explorer 浏览器,你也可以使用HTML5 dataset API 。
HTML HTML
<div id="my-div" data-info="some info here" data-other-info="more info here">My Awesome Div</div>
JavaScript JavaScript
var myDiv = document.querySelector('#my-div');
myDiv.dataset.info // "some info here"
myDiv.dataset.otherInfo // "more info here"
Demo: http://html5demos.com/dataset演示: http : //html5demos.com/dataset
Full browser support list: http://caniuse.com/#feat=dataset完整的浏览器支持列表: http : //caniuse.com/#feat=dataset
You can also use:您还可以使用:
<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"));
});
Here is the working example: https://jsfiddle.net/ed5axgvk/1/这是工作示例: https : //jsfiddle.net/ed5axgvk/1/
HTML HTML
<span id="spanTest" data-value="50">test</span>
JavaScript JavaScript
$(this).data().value;
or或者
$("span#spanTest").data().value;
ANS: 50
答案:50
Accessing the data attribute with its own id
is a bit easy for me.使用自己的
id
访问数据属性对我来说有点容易。
$("#Id").data("attribute");
function myFunction(){ alert($("#button1").data("sample-id")); }
<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>
var id = $(this).dataset.id
为我工作!
This piece of code will return the value of the data attributes.这段代码将返回数据属性的值。 For example: data-id, data-time, data-name, etc.
例如:data-id、data-time、data-name等。
I have shown it for the id:我已经为 id 展示了它:
<a href="#" id="click-demo" data-id="a1">Click</a>
JavaScript: JavaScript:
$(this).data("id");
Get the value of the data-id -> a1获取data-id的值 -> a1
$(this).data("id", "a2");
This will change the data-id -> a2这将更改数据 ID -> a2
$(this).data("id");
Get the value of the data-id -> a2获取data-id的值 -> a2
Using jQuery:使用jQuery:
$(".myClass").load(function() {
var myId = $(this).data("id");
$('.myClass').attr('id', myId);
});
Try尝试
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>
The issue is you are not specifying the option or selected option of dropdown or list, Here is an example for dropdown, i am assuming a data attribute data-record.问题是您没有指定下拉列表或列表的选项或选定选项,这是下拉列表的示例,我假设数据属性数据记录。
$('#select').on('change', function(){
let element = $("#visiabletoID");
element.val($(this).find(':selected').data('record'));
});
For those looking to dynamically remove and re-enable the tooltip, you can use the dispose
and enable
methods.对于那些希望动态删除和重新启用工具提示的人,您可以使用
dispose
和enable
方法。 See .tooltip('dispose') .请参阅.tooltip('dispose') 。
I have a span.我有一个跨度。 I want to take the value of attribute data-txt-lang , which is used defined.
我想取定义使用的属性data-txt-lang的值。
$(document).ready(function ()
{
<span class="txt-lang-btn" data-txt-lang="en">EN</span>
alert($('.txt-lang-btn').attr('data-txt-lang'));
});
for pure js对于纯js
let btn = document.querySelector('.your-btn-class');
btn.addEventListener('click',function(){
console.log(this.getAttribute('data-id'));
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.