Facebook SDK接管“$"从jQuery-如何停止

Facebook SDK takes over "$" from jQuery - how to stop that?

本文关键字:jQuery- 何停止 quot SDK 接管 Facebook      更新时间:2023-09-26

我同时使用Facebook SDK和jQuery。。。由于某种原因,随着FacebookSDK的加载,$不再作为jQuery对象工作。我所有的jQuery调用仍然有效,但我必须使用jQuery而不是$。。。请参阅下面的示例。我的页脚中还有其他代码,这些代码只是jQuery的东西,$在那里也不起作用一切都很完美,但我讨厌这样,希望能够使用$作为jQuery的快捷方式如何取回我的$

<script type='text/javascript' src='http://x.com/wp-includes/js/jquery/jquery.js?ver=1.10.2'></script>
<script>
window.fbAsyncInit = function () {
    FB.init({  // (this code straight from FB developer docs)
        appId: '1234123412341234',
        channelUrl: '//mysite.com/channel.html',
        status: true,
        cookie: true,
        xfbml: true
    });
    function checkLoginStatus(response) {
        if(response && response.status == 'connected') {
            jQuery('#login-btn').hide();
            jQuery('#logout-btn').show();
            console.log('User ID: ' + response.authResponse.userID);
        } else {
            jQuery('#login-btn').show();
            jQuery('#logout-btn').hide();
        }
    }
// Load the SDK asynchronously (this code straight from FB developer docs)
(function (d) {
    var js, id = 'facebook-jssdk',
        ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {
        return;
    }
    js = d.createElement('script');
    js.id = id;
    js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);
}(document));
</script>

只需使用一个自执行函数,并将jQuery作为参数$传递。

jQuery.noConflict();
(function ($) {
    FB.init({  // (this code straight from FB developer docs)
        appId: '1234123412341234',
        channelUrl: '//mysite.com/channel.html',
        status: true,
        cookie: true,
        xfbml: true
    });
    function checkLoginStatus(response) {
        if(response && response.status == 'connected') {
            $('#login-btn').hide();
            $('#logout-btn').show();
            console.log('User ID: ' + response.authResponse.userID);
        } else {
            $('#login-btn').show();
            $('#logout-btn').hide();
        }
    }
})(jQuery);

您可以在这里阅读更多

只有一个库可以使用$。因此,您需要将jQuery或FB API分配给其他对象。例如,你可以这样做。

window._ = window.jQuery

还有http://api.jquery.com/jQuery.noConflict/