javascript - 如果 aria-valuenow = 0,则将类添加到按钮

标签 javascript jquery function input

如果输入的 aria-valuenow 为 0,我正在尝试找到一种方法将类 testing 添加到按钮。

到目前为止我的代码:

(function() {
  if($('.plyr__volume input').attr('aria-valuenow')=="0"){
    $('.js-volume').addClass('test');
  } else {
    $('.js-volume').removeClass('test');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" class="plyr__control js-volume" data-plyr="mute"></button>
  <div class="plyr__volume">
  <input data-plyr="volume" type="range" min="0" max="1" step="0.05" value="1" 
  autocomplete="off" role="slider" aria-label="Volume" aria-valuemin="0"
  aria-valuemax="100" aria-valuenow="0" id="plyr-volume-2587" style="--value:0%; 
  width: 0px; left: 200px;" aria-valuetext="0.0%" class="plyr__tab-focus">
</div>

最佳答案

IIFE (Immediately Invoked Function Expression)

It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts. The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope.

The second part creates the immediately executing function expression () through which the JavaScript engine will directly interpret the function.

您的代码根本没有执行,在末尾指定 () 使其成为 IIFE (Immediately Invoked Function Expression) .

演示:

(function() {
  var val = $('.plyr__volume input').attr('aria-valuenow');
  if(val == "0") $('.js-volume').addClass('test');
  else  $('.js-volume').removeClass('test');
})();
.test{
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<button type="button" class="plyr__control js-volume" data-plyr="mute">My Button</button>
<div class="plyr__volume">
  <input data-plyr="volume" type="range" min="0" max="1" step="0.05" value="1" autocomplete="off" role="slider" aria-label="Volume" aria-valuemin="0"aria-valuemax="100" aria-valuenow="0" aria-valuetext="0.0%" class="plyr__tab-focus">
  
</div>

关于javascript - 如果 aria-valuenow = 0,则将类添加到按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57132663/

相关文章:

javascript - 选择 - 搜索带重音的单词

javascript - 在 react 组件内等待来自非 react 源的请求

javascript - 获取坐标: Adobe Illustrator的RGB颜色

具有多个字段的 Javascript 高级搜索

javascript - 上下文化 jQuery

python - 定义相同 Python 函数的简写?

javascript - 我有一个视频背景 Bootstrap 页面。如何制作透明叠加层?

Javascript:从函数返回html元素

c - 编译器在函数名前添加下划线前缀的原因是什么?

javascript - 什么时候应该在 JavaScript 中使用构造函数?