Javascript 如何在jquery中向链接添加属性
我想知道如何使用jquery向以下代码添加属性 退出按钮代码:Javascript 如何在jquery中向链接添加属性,javascript,jquery,Javascript,Jquery,我想知道如何使用jquery向以下代码添加属性 退出按钮代码: <div class="wp-picker-container"> <a class="wp-color-result" tabindex="0" style="background-color: rgb(111, 183, 145);" title="Select Color"></a> </div> 单击退出代码后: <div class="wp-picker-co
<div class="wp-picker-container">
<a class="wp-color-result" tabindex="0" style="background-color: rgb(111, 183, 145);" title="Select Color"></a>
</div>
单击退出代码后:
<div class="wp-picker-container">
<a class="wp-color-result wp-picker-open" tabindex="0" style="background-color: rgb(111, 183, 145);" title="Select Color"></a>
</div>
需要输出如下,如添加数据当前=当前颜色
也许有点像没有测试过
$(".wp-color-result").attr("data-current", "Current Color");
但是,如果可能的话,使用data函数可能是一个更干净的解决方案,因为data current属性很可能对页面的html标记没有任何意义。使用data函数仍然允许您在javascript代码中使用数据
数据的工作原理如下:
$(".wp-color-result").data("data-current", "Current Color");
您可以使用.data方法来实现这一点
syntax: .data(key,value)
$('.wp-color-result').on('click',function() {
$('.wp-color-result').data("current","current-color");
}
有关更多信息,请参阅:您的问题是什么?您使用此信息的速度更快了:
syntax: .data(key,value)
$('.wp-color-result').on('click',function() {
$('.wp-color-result').data("current","current-color");
}