如何在Grails 3.0中使用jQuery [英] How to Use jQuery in Grails 3.0
问题描述
我试图弄清楚如何在我的Grails 3.0应用程序中运行jQuery.我试图在我的build.gradle
I am trying to figure out how to get jQuery running in my Grails 3.0 application. I attempted adding the jQuery dependency like so in my build.gradle
runtime "org.grails.plugins:jquery:1.11.1"
当我这样做时,应用程序无法构建.我没有更改application.js文件中的任何内容,因此仍然看起来像这样:
When I did that, the application failed to build. I have not changed anything in my application.js file, so it still looks like this:
//= require jquery-2.2.0.min
//= require bootstrap
//= require_tree .
//= require_self
if (typeof jQuery !== 'undefined') {
(function($) {
$('#spinner').ajaxStart(function() {
$(this).fadeIn();
}).ajaxStop(function() {
$(this).fadeOut();
});
})(jQuery);
}
在我的gsp页面中,我同时尝试了两种方法:
Inside of my gsp pages I have tried both:
<g:javascript>
$( document ).ready(function() {
$( "#a" ).click(function( event ) {
alert( "Thanks for visiting!" );
});
});
</g:javascript>
和
<g:javascript library='jquery'>
(function($){
$( document ).ready(function() {
$( "#a" ).click(function( event ) {
alert( "Thanks for visiting!" );
});
});
})(jQuery);
</g:javascript>
第一个导致错误,指出未定义"$",第二个导致未定义jQuery.那么我应该如何在gsp页面中运行jQuery?
The first resulted in an error stating that '$' was not defined and the second said that jQuery was not defined. So how am I supposed to run jQuery inside of a gsp page?
推荐答案
grails 3附带了jquery.您必须像这样在gsp页面中进行设置:
grails 3 comes with jquery. You must set up in your gsp page like this:
<head>
<meta name="layout" content="main" />
<asset:javascript src="application.js"/>
<asset:stylesheet src="application.css"/>
<title></title>
</head>
"application.js"的资产为您的页面调用JQuery.问候.
the asset for "application.js" invokes JQuery for your page. Regards.
这篇关于如何在Grails 3.0中使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!