简体   繁体   English

如何使用 JQuery 更新数据属性?

[英]How to update data attribute using JQuery?

I am trying to use JQuery to update a data attribute after clicking on a button.单击按钮后,我正在尝试使用 JQuery 更新data属性。 Please take a look at what I have tried below.请看一下我在下面尝试过的内容。 When I click on the div with class previous-wrapper , the whole block is well updated, and the data-nb -attribute get the correct value.当我单击带有 class previous-wrapperdiv时,整个块都得到了很好的更新,并且data-nb属性得到了正确的值。 But, when I click on this div for the second time, data-nb -attribute gets the value NaN.但是,当我第二次点击这个 div 时, data-nb -attribute 的值是NaN. . .
Is there another way to dynamically update and retrieve the value of this attribute?还有另一种方法可以动态更新和检索此属性的值吗?

 $(function(){ $(document).on('click','.previous-wrapper',function(){ var get_nb = $(this).find('.previous'); get_nb = get_nb.data("nb"); get_nb = parseInt(get_nb) //Dom to update arr_left = $('<i/>', { className: 'fa-solid fa-arrow-left' }); previous = $('<div/>', { className: 'previous', 'data-nb': get_nb-5, html: "Previous" }); //Question 2. How to append previous to arrow_left $(".previous-wrapper").html(previous); }); });
 .previous-wrapper{ cursor: pointer; background:red; width:100px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="previous-wrapper"> <i class="fa-solid fa-arrow-left"></i> <span class="previous" data-nb="100">Previous</span> </div>

I would also like to know how to add multiple DOM created by JQuery.我还想知道如何添加由 JQuery 创建的多个 DOM。

You're overwriting the HTML with an invalid attribute of "classname" instead of "class", which means on the second interation $('.previous') won't match anything.您正在使用“classname”而不是“class”的无效属性覆盖 HTML,这意味着在第二次交互中$('.previous')将不匹配任何内容。

Corrected version of your code:您的代码的更正版本:

 $(function() { $(document).on('click', '.previous-wrapper', function() { var get_nb = $(this).find('.previous'); get_nb = get_nb.data("nb"); get_nb = parseInt(get_nb) //Dom to update arr_left = $('<i/>', { class: 'fa-solid fa-arrow-left' }); previous = $('<div/>', { class: 'previous', 'data-nb': get_nb - 5, html: "Previous" }); // including both elements at once: $(".previous-wrapper").html([arr_left, previous]); console.log($('.previous-wrapper').html()) }); });
 .previous-wrapper { cursor: pointer; background: red; width: 100px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="previous-wrapper"> <i class="fa-solid fa-arrow-left"></i> <span class="previous" data-nb="100">Previous</span> </div>

It would be much, much simpler, however, to simply update the one attribute you want to update, instead of rewriting all the HTML every click:但是,只需更新您要更新的一个属性,而不是每次单击都重写所有 HTML,这会简单得多:

 $(function() { $(document).on('click', '.previous-wrapper', function() { let el = $(this).find('.previous') // update the data attribute using.attr() instead of.data() because jQuery handles.data internally; it's not reflected in the DOM el.attr('data-nb', Number(el.attr('data-nb')) - 5); console.log($('.previous-wrapper').html()) }); });
 .previous-wrapper { cursor: pointer; background: red; width: 100px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="previous-wrapper"> <i class="fa-solid fa-arrow-left"></i> <span class="previous" data-nb="100">Previous</span> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM