[英]Toggle button background color on click
can some one help me with this please http://jsfiddle.net/ukkj7dth/5/ 有人可以帮我吗 ,请http://jsfiddle.net/ukkj7dth/5/
it works perfectly on toggling buttons but i don't why it's working on jsfiddle and not working in my page. 它在切换按钮上完美地工作,但是我不知道为什么它在jsfiddle上工作而不在我的页面上工作。 do i need some js file? 我需要一些js文件吗?
<div class="button">button 1</div>
<div class="button">button 2</div>
<div class="button">button 3</div>
<div class="button">button 4</div>
<div class="button">button 5</div>
<div class="button">button 6</div>
You want to fire the code on DOM ready
like so: 您想像这样在DOM ready
上触发代码:
$(function() { //<<<<----
$('.button').on('click', function() {
$(this).toggleClass('myClass').siblings().removeClass('myClass');
});
}); //<<<<----
It works in jsfiddle because you're firing the code on window load
... see onLoad
in dropdown. 它在jsfiddle中有效,因为您是在window load
触发代码的。请参阅下拉菜单中的onLoad
。 If you set jsfiddle to no wrap in <head>
it'll stop working -- which would be close to what you might have in your application. 如果将jsfiddle设置为no wrap in <head>
它将停止工作-与您的应用程序中的内容接近。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.