Ruby web开发实战(6)-JQuery(2)

发布时间 2023-06-28 14:49:20作者: 水泊waterPerl

特殊效果

一些方便的效果,使您的网站脱颖而出

var isEnglish=true;
let helloElement=document.getElementById("hello");
let $jQueryElement= jQuery(helloElement);
$jQueryElement.click(function(){
    isEnglish=!isEnglish;
    if (!isEnglish){
        $( "p").hide();
        $( "p" ).removeClass( "en" );
        $( "p" ).addClass( "zh" );
        $jQueryElement.html("你好,世界");
        $( "p").show( "slow" );
    }
    else
    {
        $( "p").hide();
        $( "p" ).removeClass( "zh" );
        $( "p" ).addClass( "en" );
        $jQueryElement.html("hello,world");
        $( "p").show( "slow" );
    }
});

传递函数

JavaScript使您能够自由地传递函数,以便稍后执行。
回调是作为参数传递给另一个函数并在其父函数完成后执行的函数。回调是特别的,因为它们耐心地等待执行,直到它们的父级完成。同时,浏览器可以执行其他功能或做各种其他工作。

如果回调没有参数,可以像这样传入

$.get( "test.html", myCallBack );

带参数的回调

$.get( "test.html", myCallBack( param1, param2 ) );