开发者

How can i make jQuery plugins compatible w/o changing the whole code?

The regular way to keep jQuery compatible to other frameworks is to override the $-Function with the following code:

jQuery.noConflict();    
jQuery(document).ready(function() {
      jQuery("someelement").dosomething();
    });

or

$j = jQuery.noConflict();    
$j(document).ready(function() {
  开发者_运维问答    $j("someelement").dosomething();
    });

but is there also a way to keep additional jquery-plugins compatible without changing the whole $-Function-Signs like above?

Thanks in advance!

Danny


The jQuery object is passed as argument to the ready handler, so you can do:

jQuery.noConflict();    
jQuery(document).ready(function($) {
    $("someelement").dosomething();
});

Regarding plugins: They should only access the global jQuery element anyway, to exactly avoid these kinds of compatibility issues.

Most plugins are defined as

(function($) {
    // plugin code here
}(jQuery));

If they are not, then they are designed badly. If you have such a plugin, you should write the author of it to fix it. You'd have to change the source and wrap the whole code inside this function to make it work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜