[英]Facebook SDK takes over “$” from jQuery - how to stop that?
由於某種原因,我同時使用了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);
你可以在這里閱讀更多
只有一個庫可以使用$。 因此您需要將jQuery或FB API分配給其他對象。 例如,您可以執行此操作。
window._ = window.jQuery
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.