1

我尝试将 jquery 用于我目前在 localhost 上测试的 Web 项目。在我的base.html我尝试通过这个链接使用谷歌服务

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js">

在我呈现的网站中,我得到了有效的标记:

<!DOCTYPE html> 
<html>
<head>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js">
 </script>

 <script>
  $(document).ready(function(){
   $("button").click(function(){
    $("p").toggle();
  });
 </script>
</head>

但是功能不起作用。我必须在开发时自己托管 jquery吗?

4

2 回答 2

1

您忘记关闭该ready功能:

$(document).ready(function(){
    $("button").click(function(){
        $("p").toggle();
    });
}); // <-- Close the function
于 2013-02-16T16:51:06.810 回答
1

是的,您可以在 django 项目中使用 jquery。此代码的问题是您忘记关闭就绪按钮

$(document).ready(function(){
    $("button").click(function(){
        $("p").toggle();
    });
});

如果您想在您的 django 项目中使用 jquery,那么我建议您在基本模板中包含 jquery 并使用该模板来创建其他页面。此外,建议创建单独的 javascript、css 和 HTML 块,以便您可以选择要包含在特定页面中的文件,这样更容易。

于 2013-02-16T17:27:43.017 回答