使用jQuery更新data属性的值 [英] Updating the value of data attribute using jQuery
本文介绍了使用jQuery更新data属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下HTML代码:
I have the following HTML code:
<a class="toggle" href="#toggle">
<img src="app/css/images/tock.png" alt="No" data-id="4" data-block="1">
</a>
我想使用jQuery更新src
和data-block
属性的值.我该怎么做?
I want to update the value of the src
and data-block
attributes using jQuery. How do I do this?
更新:由于我有很多图像元素,因此我想使用data-id
更新特定图像的值.
Update: As I have many image elements, I want to update the value of a specific image by using data-id
.
推荐答案
$('.toggle img').data('block', 'something');
$('.toggle img').attr('src', 'something.jpg');
使用 jQuery.data 和为便于理解,我将它们分别显示给您.
I'm showing them to you separately for the sake of understanding.
这篇关于使用jQuery更新data属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文