jquery 1.10并使切换事件自动运行
我有一个简单的代码,可以单击按钮并使用jquery animate color切换div大小和颜色,但问题是切换事件在没有单击的情况下发生,按钮消失 html:jquery 1.10并使切换事件自动运行,jquery,toggle,Jquery,Toggle,我有一个简单的代码,可以单击按钮并使用jquery animate color切换div大小和颜色,但问题是切换事件在没有单击的情况下发生,按钮消失 html: <input type="button" id="togglemove" value="toggle div1 move" /> <input type="button" id="toggle" value="toggle" /> <div class="div1"></div>
<input type="button" id="togglemove" value="toggle div1 move" />
<input type="button" id="toggle" value="toggle" />
<div class="div1"></div>
在JSFIDLE上直播:您没有单击事件的侦听器。也许您的代码应该如下所示:
$(document).ready(function() {
$("#toggle").click(function(){
if($(this).attr("class") != "bigger"){
click1();
$(this).addClass("bigger");
} else {
click2();
$(this).removeClass("bigger");
}
});
$("#togglemove").click(function(){
if($(this).attr("class") != "bigger"){
move1();
$(this).addClass("bigger");
} else {
move2();
$(this).removeClass("bigger");
}
});
});
您可以修改脚本,使其像 HTML:
切换的这一特殊用法是一个新版本,因此您需要自己实现切换功能。下面给出了一个示例实现 看
演示:当
文档
准备就绪时,您在就绪
功能中定义的两个切换
功能将被执行。它甚至没有在Chrome上为我显示按钮。不切换它们切换到1.83 jquery会使其工作.toggle()
函数从1.9版起被删除,这就是为什么它会把一切都搞糟。谢谢这里的一切,它是与jquery 1.10一起工作的,我不知道切换已被删除,非常感谢,但它是与jquery 1.8一起工作的,没有点击事件,我很抱歉,我只是看到我的代码不完全是你想要的。。。我会编辑它。但我认为这不可能按照你在JQ1.8中希望的方式工作。document.ready-listener与mouseclickI无关,对此我很抱歉。修复了它这不是一个建设性的答案。@Andri问题似乎是为什么它现在不起作用,并且没有其他人在他们的回答中提到这一点。不抱歉:“……但问题是切换事件发生时没有单击,按钮消失”比我的示例更好,甚至更好;)1.up@jqbeginner您可以有一个更干净的实现,如
$(document).ready(function() {
$("#toggle").click(function(){
if($(this).attr("class") != "bigger"){
click1();
$(this).addClass("bigger");
} else {
click2();
$(this).removeClass("bigger");
}
});
$("#togglemove").click(function(){
if($(this).attr("class") != "bigger"){
move1();
$(this).addClass("bigger");
} else {
move2();
$(this).removeClass("bigger");
}
});
});
<input type="button" id="togglemove" value="toggle div1 move" onclick="buttonClicked()" />
<input type="button" id="toggle" value="toggle" onclick="buttonClicked2()"/>
function buttonClicked() {
$("#togglemove").toggle(move1,move2);
}
function buttonClicked2() {
$("#toggle").toggle(click1,click2);
}
function move1(){
$(".div1").animate({"left":"200px"},1000);
}
function move2(){
$(".div1").animate({"left":"100px"},1000);
}
function click1(){
$("div.div1").animate({"width":"400px","border-radius":"10px",'background-color': '#400101','left':'200px'},1000);
}
function click2(){
$("div.div1").animate({"width":"200px","border-radius":"0",'background-color': '#000000','left':'100px'},1000);
}
$(document).ready(function(e) {
$("#toggle").click(function(){
var state = $(this).data('toggleState');
if(state){
click2();
} else {
click1();
}
$(this).data('toggleState', !state);
});
$("#togglemove").click(function(){
var state = $(this).data('toggleState');
if(state){
move2();
} else {
move1();
}
$(this).data('toggleState', !state);
});
});