【问题标题】:Script block is not working in GSP page. Any solution?脚本块在 GSP 页面中不起作用。有什么解决办法吗?
【发布时间】:2015-10-14 21:38:54
【问题描述】:

我在 GSP 页面中有脚本块,如下面的代码所示。但是脚本块没有响应。我没有在页面的控制台中打印 hello,而是收到错误 undefined operator $。可能是什么原因?

getSpecificQuestion.gsp

<html>
<head>
    <script>
            $('.index').click(function() {
                console.log("hello")
            });
    </script>
    <title> Test </title>
</head>

<body>

<div id="question">
    <%= "question = $question"%>
</div>

<div id="indexButtons">

    <g:each in="${(1..<11)}" var="i">
        <input type="button" class="index" value="${i}"/>
    </g:each>

</div>
</body>
</html>

【问题讨论】:

  • 你能检查你是否有jquery插件吗?在 BuildConfig.groovy
  • 是的,Neoryder,我已经安装了 jquery 插件。
  • $('.index').click... 放入$(document).ready...
  • 也试过了,还是不行
  • gsp 是否使用布局

标签: grails gsp


【解决方案1】:

显然您的 gsp 中没有包含任何 jquery,因此它抱怨缺少 $

确保在脚本块之前的 gsp 中包含 jquery。如果您使用资源插件,您可以执行以下操作:

<html>
<head>
  <r:require module="jQuery1111"/>
  <r:script>
    $(function() {
      $('.index').click(function() {
        console.log("hello")
      });  
    })
  </r:script>
  <title> Test </title>
</head>

<body>

<div id="question">
  <%= "question = $question"%>
</div>

<div id="indexButtons">

  <g:each in="${(1..<11)}" var="i">
    <input type="button" class="index" value="${i}"/>
  </g:each>

</div>
</body>
</html>

在所有 DOM 元素都已呈现并准备好由脚本操作后,使用该标签将在页面底部呈现您的 javascript。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多