Javascript 将jQuery转换为无冲突模式
这是我正在使用的脚本Javascript 将jQuery转换为无冲突模式,javascript,jquery,Javascript,Jquery,这是我正在使用的脚本 $(window).load(function() { $('#edifici, #artistici, #industriale, #fotovoltaico, #veterinaria, #architettonici').hide(); if (!!window.location.hash) { var hash = window.location.hash; var $content = $(hash); showContent($cont
$(window).load(function() {
$('#edifici, #artistici, #industriale, #fotovoltaico, #veterinaria, #architettonici').hide();
if (!!window.location.hash) {
var hash = window.location.hash;
var $content = $(hash);
showContent($content)
}
$('.home').click(function () {
var id = this.id.replace('mostra_', '');
var $content = $('#' + id + ':not(:visible)');
if ($('.current').length === 0) {
showContent($content)
} else {
$('.current').fadeOut(600, function () {
showContent($content)
});
}
});
function showContent(content) {
content.fadeIn(600);
$('.current').removeClass('current');
content.addClass('current');
}
});
根据,为了在WordPress中使用它(其中jQuery处于无冲突模式),我将脚本修改为
jQuery.noConflict();
jQuery(window).load(function($) {
$('#edifici, #artistici, #industriale, #fotovoltaico, #veterinaria, #architettonici').hide();
if (!!window.location.hash) {
var hash = window.location.hash;
var $content = $(hash);
showContent($content)
}
$('.home').click(function () {
var id = this.id.replace('mostra_', '');
var $content = $('#' + id + ':not(:visible)');
if ($('.current').length === 0) {
showContent($content)
} else {
$('.current').fadeOut(600, function () {
showContent($content)
});
}
});
function showContent(content) {
content.fadeIn(600);
$('.current').removeClass('current');
content.addClass('current');
}
});
不幸的是,它似乎不能正常工作。。。我做错了什么
jQuery.noConflict();
说明:放弃jQuery对$variable的控制
这意味着jQuery将不再使用$,因此它将清除与其他库的所有冲突
要在内部使用$,可以执行以下操作:
您可以将现有代码包装到匿名函数中,并将jQuery作为参数传递,例如:
(function ($) {
// use $ here
$('#hello').html('world');
})(jQuery);
或者使用jQuery提供的快捷方式:
jQuery(function($) {
// use $
$('#hello').html('world');
});
ready方法还传递jQuery对象:
jQuery(document).ready(function ($) {
// ...
});
您是否尝试过将所有的
$
替换为jQuery
?。load
不会将对jQuery的引用传递给回调:。只有.ready
会执行此操作。不会将jQuery
作为参数调用,而是将事件
对象作为参数调用。你把它和你的计划混在一起了。