I added this but gives error
I want to get and set the numerical value but it gives undefined $('#List').find('.plan').dataset.value = 10;
Thank you!
使用.attr
设置data-value
属性data-value
,该属性将设置所有.plan
的值
$('#List').find('.plan').attr("data-value", "10");
You are trying to access an DOM property on a jQuery COLLECTION.
To access the element when using dataset you need [0] as in
$('#List').find('.plan')[0].dataset.value = amt;
if you do not want to use .data("value",amt)
or .attr("data-value",amt)
which will set the amt on ANY .plan found.
Alternative you can use this
:
$('#List').find('.plan').each(function() {
this.dataset.value = amt;
})
Testing:
var amt = 10; // $('#List').find('.plan').dataset.value = amt; // does not work $('#List').find('.plan')[1].dataset.value = amt; // ONLY on second element console.log($("#List").html()) $('#List').find('.plan').attr("data-value", amt); // on ALL elements found console.log($("#List").html()) $('#List').find('.plan').each(function() { // loop over elements this.dataset.value = 20; }) console.log($("#List").html())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="List"> <li class="notPlan">Not a plan</li> <li class="plan">Plan 0</li> <li class="notPlan">Not a plan</li> <li class="plan">Plan 1</li> </ul>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.