gpt4 book ai didi

javascript - Facebook SDK 从 jQuery 接管 "$"- 如何阻止它?

转载 作者:行者123 更新时间:2023-12-04 17:04:22 27 4
gpt4 key购买 nike

我同时使用 Facebook SDK 和 jQuery...出于某种原因,加载了 Facebook SDK,$不再作为 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);

您可以阅读更多 here

关于javascript - Facebook SDK 从 jQuery 接管 "$"- 如何阻止它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18199301/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com