将"$"(美元符号)替换为"JQuery" [英] Replace "$"(dollar Sign) with "JQuery"

查看:226
本文介绍了将"$"(美元符号)替换为"JQuery"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个代码段无效.但是,当用 jQuery 替换所有 $ (美元符号)时,它开始工作(请参见第二段).但是我真的不明白为什么吗?谁能向我解释一下?非常感谢!

The 1st snippet wasn't working. However, it start working when replacing all $(dollar sign) with jQuery(See 2nd snippet). But i don't really understant why? Can anyone explain this to me? Many thanks!

第一个代码段

jQuery.noConflict();
               $(document).ready(function(){    
                    $("#insideTable > tbody > tr:odd").addClass("odd");
                    $("#insideTable > tbody > tr:not(.odd)").hide();
                    $("#insideTable > tbody > tr:odd").show();
                    $("#insideTable > tbody > tr.odd").click(function(){
                        $(this).next().toggle();
                        $(this).find(".arrow").toggleClass("up");
                    });

                });

第二个片段

        jQuery.noConflict();
        jQuery(document).ready(function(){

                jQuery("#insideTable > tbody > tr:odd").addClass("odd");
                jQuery("#insideTable > tbody > tr:not(.odd)").hide();
                jQuery("#insideTable > tbody > tr:odd").show();
                jQuery("#insideTable > tbody > tr.odd").click(function(){
                    jQuery(this).next().toggle();
                    jQuery(this).find(".arrow").toggleClass("up");
                });

            });

推荐答案

这是因为jQuery.noConflict()释放"了与jQuery相关联的"$".通常,在您的代码中,您可以使用$代替"jQuery".如果使用noConflict(),则无法再执行此操作,因此必须将每个"$"替换为"jQuery". .

This is because jQuery.noConflict() "frees" the "$" from being associated with jQuery. Normally in your code you can use $ as a replacement for "jQuery". If you use noConflict() you can't do that anymore and so you have to replace each "$" with "jQuery"; .

许多JavaScript库都使用$作为函数或变量名,只是 就像jQuery一样.在jQuery的情况下,$只是jQuery的别名,因此 无需使用$即可使用所有功能.如果我们需要使用 jQuery旁边的另一个JavaScript库,我们可以返回对 $通过调用$ .noConflict()返回另一个库:

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.noConflict():

您还可以创建一个全新的别名以使用

you can also create a totally new alias to use

var myJqueryAlias = jQuery.noConflict();
myJqueryAlias(document).ready(function(){

        myJqueryAlias("#insideTable > tbody > tr:odd").addClass("odd");
        myJqueryAlias("#insideTable > tbody > tr:not(.odd)").hide();
        myJqueryAlias("#insideTable > tbody > tr:odd").show();
        myJqueryAlias("#insideTable > tbody > tr.odd").click(function(){
            myJqueryAlias(this).next().toggle();
            myJqueryAlias(this).find(".arrow").toggleClass("up");
        });

    });

这篇关于将"$"(美元符号)替换为"JQuery"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆