admin管理员组文章数量:1424936
I am trying to make a Redmine plugin that uses jquery.
I get conflicts, when I add the following line in my view:
<script src=".6.2/jquery.min.js" type="text/javascript"></script>
Uncaught TypeError: Cannot call method 'hasChildNodes' of undefined
prototype.js:5734
Uncaught TypeError: Object [object Object] has no method 'dispatchEvent'
prototype.js:828
Uncaught TypeError: Object [object Object] has no method 'attachEvent'
The view is a hook.
If I remove the <script src="">
, it works, but then I have no jQuery to use.
I am trying to make a Redmine plugin that uses jquery.
I get conflicts, when I add the following line in my view:
<script src="http://ajax.googleapis./ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
Uncaught TypeError: Cannot call method 'hasChildNodes' of undefined
prototype.js:5734
Uncaught TypeError: Object [object Object] has no method 'dispatchEvent'
prototype.js:828
Uncaught TypeError: Object [object Object] has no method 'attachEvent'
The view is a hook.
If I remove the <script src="">
, it works, but then I have no jQuery to use.
1 Answer
Reset to default 7Looking at it, the prototype library is also loaded. Usually a library will automatically load into the global scope by assigning themselves to the $
variable.
To get jQuery working, just add $.noConflict();
after you load jQuery (see noConflict on the jQuery API). This unloads jQuery from $
and reassigns it to what it was before (prototype).
Then you access jQuery using the jQuery
object and not the $
shortcut.
jQuery(document).ready(function($) {
// Code that uses jQuery's $ can follow here.
});
If you really wanted to use a shortcut variable (for example, $j
), then you can assign it when doing the noConflict:
var $j = $.noConflict(); // Then use $j instead of $
Alternatively, you can create an anonymous function and assign jQuery to $
within it. This is only done within the scope of the function (so prototype is still $ outside of the scope):
(function($) {
$(function() {
// more code using $ as alias to jQuery
});
})(jQuery);
All of these (and a few other examples) are shown on the noConflict page - so you might want to have a look there.
本文标签: javascriptIncluding jquery in a Redmine pluginStack Overflow
版权声明:本文标题:javascript - Including jquery in a Redmine plugin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745366129a2655522.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论