避免jquery冲突

时间:2013-01-14 14:13:23

标签: jquery jquery-plugins

我使用jquery来缩放图像,它在测试html

上工作正常

但它显示错误,当我尝试在实时网站中实现它时看起来像冲突错误,因为其他js也调用它,它显示以下错误

  

Uncaught TypeError: Object #<HTMLDocument> has no method 'ready' *位于base.js

的第1行

我尝试下面的代码以避免冲突,但它不起作用看起来我错过了一些东西

jquery.noconflict();
$(document).ready(on_ready);
function on_ready() {
$('#search').defaultVal('search here...');
$('#comment').defaultVal('your comment here...');
$('#message').defaultVal('your message here...');
.....

base.js文件可以在下面提到的链接中看到

http://techchef.co/devTest/test/base.js

4 个答案:

答案 0 :(得分:2)

jQuery.noConflict();
// cannot use '$' because of .noConflict()
// now the '$'-sign is free for other libraries e.b. MooTools...
// ...that's what noConflict does

// try using 'jQuery' instead of '$' here
jQuery(document).ready(function() {
    jQuery('.selector').click(function() {
        // and so on
    });
});

// or an other solution looks like this

(function($) {
    $(function() {
        // more code using $ as alias to jQuery
    });
})(jQuery)

// in your case try

jQuery(document).ready(function() {
    // the code you have in your on_ready-function
});

// or

jQuery(document).ready(function() {
    on_ready();
});
function on_ready() {
    // your on_ready-function
}

MORE INFO

答案 1 :(得分:0)

var j = jQuery.noConflict();
// Do something with jQuery
j("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';

see this documentation for more information.

答案 2 :(得分:0)

当我尝试这样做时,它会显示错误,&amp;在我呼叫其呼叫的页面上没有检测到函数调用

jQuery.noConflict() 
jQuery(document).ready(function($)
{ 
function on_ready() { $('#search').defaultVal('search here...');     
$('#comment').defaultVal('your comment here...'); 
$('#message').defaultVal('your message here...'); }); 

})

实际上默认情况下,base.js会调用on_ready函数,如下所示

$(document).ready(on_ready); 
function on_ready() {
 $('#search').defaultVal('search here...');
 $('#comment').defaultVal('your comment here...');
 $('#message').defaultVal('your message here...'); 

答案 3 :(得分:0)

我刚刚在$

中将jQuery替换为base.js,我就解决了这个问题

它对我有用。希望它有所帮助。