Bru*_*ams 762 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"));
});
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
Mar*_*rth 1534
要获取属性的内容data-id
(如in <a data-id="123">link</a>
),您必须使用
$(this).attr("data-id") // will return the string "123"
Run Code Online (Sandbox Code Playgroud)
或者.data()
(如果你使用更新的jQuery> = 1.4.3)
$(this).data("id") // will return the number 123
Run Code Online (Sandbox Code Playgroud)
并且后面的部分data-
必须是小写的,例如data-idNum
不起作用,但data-idnum
会.
Lal*_*rya 160
如果我们想使用现有的原生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>
Run Code Online (Sandbox Code Playgroud)
通过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');
Run Code Online (Sandbox Code Playgroud)
Ser*_*ltz 80
重要的提示.请记住,如果您通过JavaScript动态调整data-
属性,它将不会反映在data()
jQuery函数中.你必须通过data()
功能调整它.
<a data-id="123">link</a>
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Ron*_*oni 52
如果您不关心旧的IE浏览器,也可以使用HTML5数据集API
HTML
<div id="my-div" data-info="some info here" data-other-info="more info here">My Awesome Div</div>
Run Code Online (Sandbox Code Playgroud)
JS
var myDiv = document.querySelector('#my-div');
myDiv.dataset.info // "some info here"
myDiv.dataset.otherInfo // "more info here"
Run Code Online (Sandbox Code Playgroud)
演示:http://html5demos.com/dataset
完整的浏览器支持列表:http://caniuse.com/#feat=dataset
cur*_*Boy 16
惊讶没人提到:
<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"));
});
Run Code Online (Sandbox Code Playgroud)
以下是工作示例:https://jsfiddle.net/ed5axgvk/1/
bam*_*sza 12
HTML
<span id="spanTest" data-value="50">test</span>
Run Code Online (Sandbox Code Playgroud)
JS
$(this).data().value;
Run Code Online (Sandbox Code Playgroud)
要么
$("span#spanTest").data().value;
Run Code Online (Sandbox Code Playgroud)
ANS:50
适合我!
Gje*_*ahl 11
我使用$ .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
Run Code Online (Sandbox Code Playgroud)
Ara*_*opi 10
使用它自己访问数据属性id
对我来说有点容易.
$("#Id").data("attribute");
function myFunction(){
alert($("#button1").data("sample-id"));
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
小智 5
使用jQuery:
$(".myClass").load(function() {
var myId = $(this).data("id");
$('.myClass').attr('id', myId);
});
Run Code Online (Sandbox Code Playgroud)
这段代码将返回数据属性的值,例如:data-id,data-time,data-name等。
<a href="#" id="click-demo" data-id="a1">Click</a>
Run Code Online (Sandbox Code Playgroud)
js:
$(this).data("id");
Run Code Online (Sandbox Code Playgroud)
//获取data-id的值-> a1
$(this).data("id", "a2");
Run Code Online (Sandbox Code Playgroud)
//这将更改data-id-> a2
$(this).data("id");
Run Code Online (Sandbox Code Playgroud)
//获取data-id的值-> a2
尝试
this.dataset.id
Run Code Online (Sandbox Code Playgroud)
this.dataset.id
Run Code Online (Sandbox Code Playgroud)
$("#list li").on('click', function() {
alert( this.dataset.id );
});
Run Code Online (Sandbox Code Playgroud)