简体   繁体   English

向上一个和下一个按钮添加一个类

[英]Adding a class to the previous and next buttons

I have a pagination script, where I need to append classes to the previous and next buttons. 我有一个分页脚本,需要在其中将类附加到上一个和下一个按钮。

Code for the same as below. 代码如下。

document.querySelector('.prev').style.opacity=(turntopage == 1)? "0.1" : "1";
document.querySelector('.next').style.opacity=(turntopage == totalpages)? "0.1" : "1";

Currently, on click of previous and next, the visibility is being applied from the above which is rendered as below. 当前,单击上一个和下一个,从上方开始应用可见性,如下所示。

<a class="next" href="#next" style="opacity: 0.1;">
<a class="next" href="#next" style="opacity: 1;">

I need to make changes to the above so instead of style, I can append the class which should be rendered as below. 我需要对上面的内容进行更改,因此可以代替样式,而可以附加如下所示的类。

<a class="next disabled" href="#next">

Complete demo can be seen HERE 完整的演示可以在这里看到

You can use the classList property with the .add and .remove functions: 您可以将classList属性与.add和.remove函数一起使用:

document.querySelector('.next').classList.add('disabled'); document.querySelector('。next')。classList.add('disabled');

Thanks for the support. 感谢您的支持。 I solved it myself. 我自己解决了。 I created an ID and added it the same way I had style. 我创建了一个ID并以与样式相同的方式添加了它。 It resolved my purpose. 它解决了我的目的。 Initially, as per suggestions of the people, I did try .classList.add , .addClass , .className += , etc., but that created multiple instances/duplication of the classes on click of previous and next and that made the mark-up look very messy. 最初,按照人们的建议,我确实尝试了.classList.add.addClass.className +=等,但是在单击上一个和下一个时创建了类的多个实例/重复项,并标记为起来看起来很乱。 This works smoothly. 这样可以顺利进行。 Let me know for any suggestions/edits. 让我知道任何建议/编辑。

Changes in the code as below. 代码更改如下。

document.querySelector('.prev').id=(turntopage == 1)? "disabled" : "enabled";
document.querySelector('.next').id=(turntopage == totalpages)? "disabled" : "enabled";

LIVE DEMO OF THE SOLUTION 解决方案的实时演示

Either I don't get it, or I don't know what to think... 我不明白,或者我不知道该怎么想...

$( "a" ).addClass( "addClass1 addClass2" ); $(“ a”).addClass(“ addClass1 addClass2”);

change the first part to change how the tag matching is done (by element, by id, etc).. but this seems too easy, so most likely i'm missing the point... 更改第一部分以更改标记匹配的方式(按元素,按ID等)..但这似乎太容易了,所以很可能我错过了重点...

upd: missed the no jquery.. use plain js.. var d = document.getElementById("a"); upd:错过了没有jquery。使用纯js。var d = document.getElementById(“ a”); d.className = d.className + " disabled"; d.className = d.className +“已禁用”;

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

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