1. 程式人生 > >解決velocity與jquery的衝突

解決velocity與jquery的衝突

1、使用jQuery代替$. 如:jQuery.ajax();

缺點:不適合擴充套件,一旦替換成第三方庫時,那就麻煩大發

2、使用jQuery.noConflict。 如:var j = jQuery.noConflict(); j.ajax();

缺點:當使用jQuery的相關外掛時,會使得外掛失效哦!

3、wrap jQuery中的衝突方法。

如$.ajax()在Velocity中會衝突,則重新定義如下:

function dw(){}

dw.ajax=function(s){ jQuery.ajax(s); } dw.ajax();

方案3基本上解決了1、2中的缺點~~~故推薦使用第三種方法!

4、

定義一個 $JQ="$."

以後可以用 ${JQ}ajax().......

<script type="text/javascript">
var PRICE_FORMAT = '&yen;%s';
$(function(){
    //首頁左側分類選單
$(".category ul.menu").find("li").each(
            function() {
              $(this).hover(
                      function() {
                        var 
cat_id = $(this).attr("cat_id"); var menu = $(this).find("div[cat_menu_id='"+cat_id+"']"); menu.show(); $(this).addClass("hover"); var menu_height = menu.height(); if (menu_height < 60) menu.height(80); menu_height = menu.height(); var li_top = $(this).position().top; $(menu).css("top",-li_top + 38
); }, function() { $(this).removeClass("hover"); var cat_id = $(this).attr("cat_id"); $(this).find("div[cat_menu_id='"+cat_id+"']").hide(); } ); } ); $(".head-user-menu dl").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); }); $('.head-user-menu .my-mall').mouseover(function(){// 最近瀏覽的商品 load_history_information(); $(this).unbind('mouseover'); }); $('.head-user-menu .my-cart').mouseover(function(){// 執行載入購物車 load_cart_information(); $(this).unbind('mouseover'); }); $('#button').click(function(){ if ($('#keyword').val() == '') { if ($('#keyword').attr('data-value') == '') { return false } else { window.location.href="http://b2b2c.shopnctest.com/test/shop/index.php?act=search&op=index&keyword="+$('#keyword').attr('data-value'); return false; } } }); $(".head-search-bar").hover(null, function() { $('#search-tip').hide(); }); // input ajax tips $('#keyword').focus(function(){$('#search-tip').show()}).autocomplete({ //minLength:0, source: function (request, response) { $.getJSON('http://b2b2c.shopnctest.com/test/shop/index.php?act=search&op=auto_complete', request, function (data, status, xhr) { $('#top_search_box > ul').unwrap(); response(data); if (status == 'success') { $('#search-tip').hide(); $(".head-search-bar").unbind('mouseover'); $('body > ul:last').wrap("<div id='top_search_box'></div>").css({'zIndex':'1000','width':'362px'}); } }); }, select: function(ev,ui) { $('#keyword').val(ui.item.label); $('#top_search_form').submit(); } }); $('#search-his-del').on('click',function(){$.cookie('9204_his_sh',null,{path:'/'});$('#search-his-list').empty();}); }); </script>
將$.改為jQuery就不會報錯
  source: function (request, response) {
    jQuery.getJSON('http://b2b2c.shopnctest.com/test/shop/index.php?act=search&op=auto_complete', request, function (data, status, xhr) {
      $('#top_search_box > ul').unwrap();
response(data);
if (status == 'success') {
        $('#search-tip').hide();
$(".head-search-bar").unbind('mouseover');
$('body > ul:last').wrap("<div id='top_search_box'></div>").css({'zIndex':'1000','width':'362px'});
}
    });
},
select: function(ev,ui) {
    $('#keyword').val(ui.item.label);
$('#top_search_form').submit();
}
});
$('#search-his-del').on('click',function(){jQuery.cookie('9204_his_sh',null,{path:'/'});$('#search-his-list').empty();});