noUiSlider 获取滑动事件调用者的 Id

noUiSlider get Id of slide event caller

我在使用 noUislider 时遇到问题,因为我需要能够在触发滑动事件时区分不同的滑块。

我认为(通过阅读 noUiSlider GitHub)我可以使用 'this' 参考,但它似乎不起作用。

我的代码:

var mix_slider = document.getElementsByClassName('slider');

for (var i = 0; i < mix_slider.length; i++) 
{
noUiSlider.create(mix_slider[i], {
  start: [100],
  connect: "lower",
  orientation: "vertical",
  direction: "rtl",
  range: {
    'min': 0,
    'max': 127
  }
});

mix_slider[i].noUiSlider.on('slide', function(values, handle){
  var id = this.id; //<-- Needs to be the id of the slider that triggered the slide event 
  var part = id.slice(-2);
  var val = this.noUiSlider.get();//<-- This doesn't seem to work either
  document.getElementById('input_' + part).value = val;
});
};

我不确定我做错了什么...我(在以前的 noUiSlider 版本上)使用了 jQuery 的 $(this).attr('id');获取 id,但这也不起作用。

如有任何帮助,我们将不胜感激

noUiSlider事件中的this是滑块API。您可以使用 this.target 获取 API 中的滑块元素。 (要获取元素 ID,请使用 this.target.getAttribute('id')

var val = this.noUiSlider.get(); //<-- This doesn't seem to work either

由于 this 是 API,您可以 this.get()