如何使用 javascript 打开 onload

how to make toggle on onload with javascript

我在我的网站上使用了一个开关,我想使用 javascript 来控制它。到目前为止它只适用于 html 和 css 而没有 javascript。我想用 javascript 控制它,并使其在页面加载时为 "on" 而不是关闭。

html

<label class="switch">
    <input type="checkbox" id="toggle-event">
    <span class="slider round"></span>
</label>

css

.switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 34px;
}

.switch input {
    display: none;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    -webkit-transition: .4s;
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 26px;
    width: 26px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    -webkit-transition: .4s;
    transition: .4s;
}

input:checked + .slider {
    background-color: #0000FF;
}

input:focus + .slider {
    box-shadow: 0 0 1px #0000FF;
}

input:checked + .slider:before {
    -webkit-transform: translateX(26px);
    -ms-transform: translateX(26px);
    transform: translateX(26px);
}

.slider.round {
    border-radius: 34px;
}

.slider.round:before {
    border-radius: 50%;
}

我试着让它像这样工作,但它不起作用。

($("#toggle-event").prop("checked"))

($("#toggle-event").toggle("show"))

这里是jsfiddle

感谢任何帮助!

试试这个:

$("#toggle-event").attr('checked', 'checked');

编辑:

如 hungerstar 所述,您缺少 prop() 方法的第二个参数。 使用 .prop().attr()

正确

注意:在您的样本中 .toggle() 是完全不正确的。该方法适用于 showing/hiding 元素,永远不会起作用。 http://api.jquery.com/toggle/

prop() 接受第二个参数来设置 属性 的值。目前您只能获取 属性 checked 的值。尝试

$( '#toggle-event' ).prop( 'checked', true );

还要确保将 JS 放在 </body> 之前或 $( ...your code... ) 中,以便在元素的 DOM 存在时执行它。

$( '#toggle-event' ).prop( 'checked', true );
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  display: none;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked+.slider {
  background-color: #0000FF;
}

input:focus+.slider {
  box-shadow: 0 0 1px #0000FF;
}

input:checked+.slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
    <input type="checkbox" id="toggle-event">
    <span class="slider round"></span>
</label>


您还可以将 checked 属性添加到您的元素,以便从一开始就将其置于选中状态。

<input type="checkbox" id="toggle-event" checked="checked">

HTML

<div class="switch"> <label for="mycheckbox" class="switch-toggle" data-on="On" data-off="Off"></label> <input type="checkbox" id="mycheckbox" /> </div>

包含复选框和标签的元素应指定 class 开关。 label 元素需要 class switch-toggle 以及 data-on 和 data-on 属性来定义两种状态的标签文本。

重要的是 .switch 元素只包含一个复选框,并且复选框和标签是 .switch 元素的直接子元素。

CSS

label.switch-toggle { 
    background: url('switch.png') repeat-y; 
    display: block !important; 
    height: 12px; 
    padding-left: 26px; 
    cursor: pointer; 
    display: none; 
    } 

label.switch-toggle.on { 
background-position: 0 12px; 
} 

label.switch-toggle.off {
 background-position: 0 0;
 } 

label.switch-toggle.hidden { 
display: none; 
}

Jquery

$(document).ready(function() {
        $('.switch').each(function() {
            var checkbox = $(this).children('input[type=checkbox]');
            var toggle = $(this).children('label.switch-toggle');
            if (checkbox.length) {
                checkbox.addClass('hidden');
                toggle.removeClass('hidden');
                if (checkbox[0].checked) {
                    toggle.addClass('on');
                    toggle.removeClass('off');
                    toggle.text(toggle.attr('data-on'));
                } else {
                    toggle.addClass('off');
                    toggle.removeClass('on');
                    toggle.text(toggle.attr('data-off'));
                };
            }
        });
        $('.switch-toggle').click(function() {
                    var checkbox = $(this).siblings('input[type=checkbox]')[0];
                    var toggle = $(this); // We need to inverse the logic here, because at the time of processing, // the checked status has not been enabled yet. if (checkbox.checked) { toggle.addClass('off'); toggle.removeClass('on'); toggle.text(toggle.attr('data-off')); } else { toggle.addClass('on'); toggle.removeClass('off'); toggle.text(toggle.attr('data-on')); }; }); });

只需将选中的属性放在输入元素上。不需要 javascript 代码。

已更新HTML

<label class="switch">
<input checked type="checkbox" id="toggle-event">
<span class="slider round"></span>
</label>

JS Fiddle link: https://jsfiddle.net/0kzzcrnq/