简体   繁体   English

如果数据属性为真,则添加 class

[英]if data attribute is true, add class

I want buttons in a slider to get underlined when a slide is visible.我希望 slider 中的按钮在幻灯片可见时带有下划线。 I think I need to check if a data attribute is true, and then add class.我想我需要检查一个数据属性是否为真,然后添加 class。

When inspecting my webpage, I find this in properties > dataset: DOMStringMap > isactiveslide: "true"检查我的网页时,我在属性 > 数据集:DOMStringMap > isactiveslide: "true" 中找到了这个

I need to check if a slide has isactiveslide: "true" (or even data-isactiveslide: "true") and then add class.我需要检查幻灯片是否有 isactiveslide:"true"(甚至是 data-isactiveslide:"true"),然后添加 class。

I think I am close and have tried these two codes:我想我很接近并尝试了这两个代码:

jQuery(function () {
  if (jQuery('menu1').attr('isactiveslide') === true) {
    jQuery(this).find("#test1").addClass("underline");
  }
})

and

jQuery('menu1').each(function(){
    if(jQuery(this).attr('isactiveslide')==true())
        jQuery('#test1').addClass('underline');
})

EDIT (added after some great answers and questions)编辑(在一些很好的答案和问题之后添加)

And here is the section, where the data attribute "isactiveslide" occurs, copied from the page:这是从页面复制的数据属性“isactiveslide”出现的部分:

<rs-slide data-key="rs-1898" data-title="WORKS" data-in="o:0;" data-out="a:false;" class="menus works1" id="works1" data-originalindex="2" data-origindex="1" data-description="" data-sba="" data-scroll-based="false" style="overflow: hidden; height: 100%; width: 100%; z-index: 20; opacity: 1; visibility: inherit;" data-owidth="300" data-oheight="200" data-rspausetimeronce="0" data-isactiveslide="true"><

So, the next slide which is not yet shown has data-isactiveslide="false".因此,下一张尚未显示的幻灯片具有 data-isactiveslide="false"。 I reckon, identifying "true" is how I can add class.我认为,识别“真实”是我添加 class 的方法。

EDIT May 4th - I think I am close now, but it still does not work.编辑 5 月 4 日 - 我想我现在很接近了,但它仍然不起作用。

jQuery('#slide1[data-isactiveslide="true"]')("#slide1-btn").addClass('.underline');

any help is very appreciated!非常感谢任何帮助!

Can be easily done by css: You need to find the class applied on the active slide and button可以通过 css 轻松完成:您需要找到应用在活动滑块和按钮上的 class

rs-slide.menus[data-isactiveslide="true"] .button-class-name-here{
       text-decoration:underline!important;
}

or或者

Find which slider you are using and on the slide change event of that slider apply the class on the button for styling.找到您正在使用的 slider 并在该 slider 的幻灯片更改事件上应用 class 按钮进行样式设置。

Try this code:试试这个代码:

 var $ = document.querySelectorAll.bind(document) //No need for jquery - simply import the function $(".menu1[data-is-active-slide]").forEach((el, index) => { $("#test1")[index].classList.add('underline'); $("#test1")[index].innerText = "Selected;". console;log(1); })
 <div class="menu1" data-is-active-slide='true'>1</div> <div id="test1"></div> <div class="menu1" data-is-active-slide='false'>2</div> <div id="test1"></div> <div class="menu1">3</div> <div class="menu2" data-is-active-slide='false'>4</div> <div class="menu2">5</div> <div class="menu1" data-is-active-slide>6</div> <div id="test1"></div> <div class="menu2">7</div> <div class="menu1 menu2" data-is-active-slide="true">8</div> <div id="test1"></div> <div class="menu1 menu2">9</div>

The beginning declaration of $ is simply defining it since I did not import jQuery. $的开头声明只是定义它,因为我没有导入 jQuery。

The next part is where the 'fun' begins.下一部分是“乐趣”的开始。 I used $(".menu1[data-is-active-slide]") to select all elements with class menu1 and with the property that data-is-active-slide is present.我使用$(".menu1[data-is-active-slide]")到 select 所有元素与 class menu1并具有data-is-active-slide存在的属性。 Then, I simply defined an action inside the function, for the sake of demonstrating that it works.然后,我简单地在 function 中定义了一个动作,以证明它有效。

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

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