0

我正在尝试为我的 symfony 平台设置一个简单的 jquery。只是试图实现这个http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide_p看看是否一切正常。

base.html.twig

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>{% block title %}TEST JQUERY{% endblock %}</title>
    {% block stylesheets %}{% endblock %}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
</head>
<body>
    {% block body %}{% endblock %}
    {% block javascripts %}{% endblock %}
</body>
</html>

index.html.twig

{% extends '::base.html.twig' %}

{% block body %}

    <script type="text/javascript">
        $(document).ready(function(){
            $("button").click(function(){
                $("p").hide();
            });
        });
    </script>


<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>


{% endblock %}

当我在页面上查看源代码时,结构似乎很好,但 jquery 没有被执行。

4

3 回答 3

2

google 的 CDN 上不存在您包含的 jQuery

改为使用<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

如果您想要版本,请通过此链接1.9.1jquery.com获取

于 2013-02-05T10:37:51.997 回答
1

更改 jquery 函数并尝试以下操作:

<script type="text/javascript">
    $(document).ready(function(){
        $("button").live('click', function(){
            $("p").hide();
        });
    });
</script>

更新

作为rrikesh指针,因为 live 在 v1.7 之后被弃用,你可能想要使用这个:

<script type="text/javascript">
    $(document).on("click", "button", function (){
        $("p").hide();
    });
</script>
于 2013-02-05T10:33:29.320 回答
0

这可能是一个更好的解决方案:

<script src="{{ asset('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js') }}"></script>
<script><window.jQuery || document.write('<script src=\"\{\{ asset(\'../app/Resources/public/js/vendor/jquery-1.9.1.min.js\') \}\}\"><\/script>')</script>

并将文件下载jquery-1.9.1.min.js到您的app/Resources/public/js/vendor/目录。

这样,客户端总是得到源。

于 2013-10-21T23:23:09.407 回答