supesite 等程序中 $ 函數與 jquery 衝突的解決辦法

1.替換選擇器函數javascript

將原程序中的:html

function $(id) {java

    return document.getElementById(id);jquery

}函數

 

替換爲:spa

function $(id) {htm

    if (typeof jQuery == 'undefined' || (typeof id == 'string' && document.getElementById(id))) {對象

        return document.getElementById(id);ip

    } else if (typeof id == 'object' || !/^\w*$/.exec(id) || 
                            /^(body|div|span|a|input|textarea|button|img|ul|li|ol|table|tr|th|td)$/.exec(id)){開發

        return jQuery(id);

    }

    return null;

}

作一個兼容,對於之前直接用字符串ID的調用依舊使用document.getElementById(id)去獲取DOM對象;而若是傳入的ID是對象,或者裏面有特殊符號(如 # . : ^ 等 jQuery 選擇器符號)或者是經常使用的html標籤,就使用jQuery選擇器去獲取jQuery對象。

 

2.須要先加載jquery的庫,而後加載聲明這個兼容的 $ 函數的js文件,以覆蓋掉jquery的 $ 函數。

<script language="javascript" type="text/javascript" src="source/script_jquery.js"></script>

<script language="javascript" type="text/javascript" src="source/script_common.js"></script>

…………

 

假若有個TAG的ID是'ctrl_with_id',使用 $('ctrl_with_id') 取得的是DOM的對象,使用$('#ctrl_with_id')可取得jQuery對象,互相不會衝突。

如此,原來程序中的 $ 函數依舊工做,並且同時能夠使用 $ 做爲jQuery的選擇器。

 

這麼解決衝突,暫時還未發現問題。

後續有不少開發填坑的文章發佈,若是對你有幫助,請支持和加關注一下

http://e22a.com/h.05ApkG?cv=AAKHZXVo&sm=339944

https://shop119727980.taobao.com/?spm=0.0.0.0 

相關文章
相關標籤/搜索