简体   繁体   English

无法使用Bootstrap滑块更改不透明度值

[英]Cannot change opacity value using Bootstrap slider

For some reason, the slider will not change the value, I keep getting " Uncaught TypeError: Cannot set property 'opacity' of undefined " errors in the browser console. 由于某些原因,滑块将不会更改该值,我会在浏览器控制台中不断收到“ Uncaught TypeError: Cannot set property 'opacity' of undefined ”错误。

I just want to change the value of opacity from the initial value of 1, to the value of the slider multiplied by .01..... 我只想将不透明度的值从初始值1更改为滑块值乘以.01 .....

my script below: 我的脚本如下:

<div id="jpegcam" class="jpegcam_slider" style="opacity: 1; z-index: 1; position: absolute; margin-left: auto; margin-right: auto; left: 0; right: 0; top: 239px"></div>

<input id="ex1" data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="100"/>
            <script>
                $('#ex1').slider({
                    formater: function(opacity) {
                        return 'Current value: ' + opacity;
                    }
                });
                $('#ex1').on('slide', function(value)
                {
                    $('.jpegcam_slider').style.opacity = (value*.01);
                });
            </script>

Been at it for a good hour or two looking up on Google and forums but coming up short... 在一两个小时的时间里一直在Google和论坛上查询,但很快就会出现...

What am I doing wrong? 我究竟做错了什么? Thanks 谢谢

PS I'm using the slider bootstrap js and css plugin from here . PS我从这里使用滑块bootstrap js和css插件。

您可以使用css()代替,因为$('.jpegcam_slider')是jQuery对象:

$('.jpegcam_slider').css('opacity', value*.01);

You're combining jQuery with generic JavaScript. 您正在将jQuery与通用JavaScript结合在一起。

You need to change this line: 您需要更改此行:

$('.jpegcam_slider').style.opacity = (value*.01);

To either get the native element object from the jQuery object (using [0] ): 要从jQuery对象获取本机元素对象(使用[0] ):

$('.jpegcam_slider')[0].style.opacity = (value*.01);

Or use jQuery's built-in CSS method : 或使用jQuery的内置CSS方法

$('.jpegcam_slider').css({ opacity: value*.01 });

See http://try.jquery.com and http://learn.jquery.com to understand the basics. 请参阅http://try.jquery.comhttp://learn.jquery.com以了解基础知识。

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

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