简体   繁体   English

如何使用 jQuery 更改数据图标值?

[英]How can I change the data-icon value using jQuery?

I have an ajax "load more/show less" button on my blog.我的博客上有一个 ajax“加载更多/显示更少”按钮。 The default button text is "More Posts" with a down arrow icon.默认按钮文本是带有向下箭头图标的“更多帖子”。

More Posts button更多帖子按钮

When the button is clicked, the text changes to "Less Posts", but I can't figure out how to change the icon to an up icon.单击按钮时,文本变为“Less Posts”,但我不知道如何将图标更改为向上图标。

Less Posts button减少帖子按钮

In the code below, I need to change the value of data-icon="7" to data-icon="6" .在下面的代码中,我需要将data-icon="7"的值更改为data-icon="6"

<a class="et_pb_button et_pb_custom_button_icon el-pagination-button el-button el-show-less" href="https://wbj1.flywheelsites.com/blog/?el_dbe_page" data-icon="7">Less Posts</a>

I tried this jQuery snippet without any luck, but I'm not very skilled in javascript or jQuery so it could be wrong:我试过这个 jQuery 片段没有任何运气,但我对 javascript 或 jQuery 不是很熟练,所以它可能是错误的:

document.getElementByClassName("el-show-less").setAttribute("data-icon"), "6");

If you want to change the value of data-icon using pure Javascript you can handle that like this如果您想使用纯 Javascript 更改data-icon的值,您可以像这样处理

let element = document.querySelector('.el-show-less');
element.dataset.icon = 7;

Using jQuery you can perform that like this使用 jQuery 你可以像这样执行

let element = $('.el-show-less');
element.data('icon', 4);

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

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