简体   繁体   English

如何使用JavaScript切换某些jQuery效果

[英]how to toggle certain jquery effects with javascript

I want to be able to toggle certain effects in jquery by using javascript, is this code correct? 我希望能够通过使用javascript在jquery中切换某些效果,此代码正确吗? and what would be the simplest solution? 最简单的解决方案是什么?

Ps dont say "use .toggle()", there is currently a bug with it that causes it to minimise the paragraph ps不会说“ use .toggle()”,当前存在一个错误,导致其最小化了该段

HTML HTML

<!--This first line calls in jquery-->
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<p>Hello all</p>

CSS CSS

p {
    color: red;
    background-color: lightblue;
    border-radius: 5px;
    height: 50px
}

JQUERY/JAVASCRIPT JQUERY / JAVASCRIPT

var num = 0;

$(document).ready(function () {
    $("p").click(
    if (num == 0) {
        var num == 1;
    } else {
        var num == 0;);

    $("p").click(function () {
        if (num == 1) {
            $(this).animate({
                color: "black",
                backgroundColor: "white",
                fontFamily: "arial",
                fontSize: "20px",
                fontFamily: "serif"
            }, 500);
        } else {
            $(this).animate({
                color: "red",
                backgroundColor: "lightblue",
                borderRadius: "5px",
                height: "50px",
            }, 500);
        });
    });

Thank you all for your time i really appreciate it. 谢谢大家的宝贵时间。

The easiest way to do that is probably with a data attribute as a flag, and with ternarys in the object, like this 最简单的方法可能是将data属性作为标志,并在对象中使用三元,就像这样

$('p').on('click', function() {
    var flag = $(this).data('flag');

    $(this).animate({
        color           : flag ? "red"     : "black",
        backgroundColor : flag ? "#ADD8E6" : "white",
        fontSize        : flag ? "20px"    : "10px",
        borderRadius    : flag ? "5px"     : "0px"
    });

    $(this).data('flag', !flag);
});

FIDDLE 小提琴

As to why your code is not working, you are just using the wrong operators in your first click handler and not defining a function, the right code should be: 至于为什么代码无法正常工作,您只是在第一次单击处理程序中使用了错误的运算符,而没有定义函数,因此正确的代码应为:

var num=0;

$(document).ready(function () {

$("p").click(function(){
    if (num == 0) {
        num = 1;
    } else {
        num = 0;
    }
});

$("p").click(function () {
    if (num == 1) {
        $(this).animate({
            color: "black",
            backgroundColor: "white",
            fontFamily: "arial",
            fontSize: "20px",
            fontFamily: "serif"
        }, 500);
    } else {
        $(this).animate({
            color: "red",
            backgroundColor: "lightblue",
            borderRadius: "5px",
            height: "50px",
            fontSize: "12px",
        }, 500);
    }
});

});

However I much prefer @adeneo's solution as it's more elegant and less verbose 但是我更喜欢@adeneo的解决方案,因为它更优雅,更不冗长

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

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