【问题标题】:Can I use jquery while developing a Django project?我可以在开发 Django 项目时使用 jquery 吗?
【发布时间】:2013-02-16 16:46:03
【问题描述】:

我尝试将 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>

但功能不起作用。我必须host jquery myself while developing吗?

【问题讨论】:

  • 使用浏览器控制台查看抛出的错误...会看到syntax error

标签: jquery django localhost


【解决方案1】:

你忘了关闭ready函数:

$(document).ready(function(){
    $("button").click(function(){
        $("p").toggle();
    });
}); // <-- Close the function

【讨论】:

  • 嘘*****t。真的。今天是星期六晚上,我应该关掉电脑,穿上我的舞鞋。谢谢!
  • 很高兴它有帮助!如果它有助于解决您的问题,请将答案标记为已接受。谢谢:)
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-03
    • 2020-06-08
    • 2019-03-01
    • 2013-08-07
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多