Django JQuery项目不工作 [英] Django JQuery items dont work

查看:280
本文介绍了Django JQuery项目不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是检查JQuery在我的Django项目中是否正常工作这里的一个例子是一个例子。当我为一个项目放一个事件它不工作。看到我的代码,它将是清楚的。看来,问题出现在我调用一个项目,像这样#text



(1)test.js WORKING:

  $(document).ready(function(){
alert(Ready);
});

(2)test.js NOT WORKING:



($($)$($)$($)$($)$($)$($) );
});
});

homepage.html:

 < DIV> 
< script type =text / javascriptsrc ={%staticjs / jquery.js%}>< / script>
< script type =text / javascriptsrc ={%staticjs / test.js%}>< / script>
< p id =text>点击隐藏< / p>
< / div>我在第2行的test2.js中尝试过:$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $文本')
$(text) $('text') ...

解决方案

如果是延迟事件绑定的结果,请尝试使用jQuery的事件委托通过 on()函数。这将转换您现有的

  $(document).ready(function(){

$ #text)。click(function(){
alert(Text Clicked);
});

});

into

  $(document).ready(function(){

$(#text)。on(click,function(){
alert );
});

});

在此期间,我建议您修改模板以使 < script> < head> 中的标签

 <!DOCTYPE html> 
< html>

< head>

< title> APP TITLE< / title>
< link href =/ path / to / css =stylesheet>
< script type =text / javascriptsrc ={%staticjs / jquery.js%}>< / script>

< / head>

< body>
< p id =text>点击隐藏< / p>
< / body>

< / html>


I am just checking if JQuery is working correctly in my Django project The one here is an exmple .When i put an event for an item it doesn't work.See my code and it ll be clear.It seems that the problem occurs when i call an item, like here "#text"

(1) test.js WORKING:

$(document).ready(function(){
  alert("Ready");
}); 

(2) test.js NOT WORKING :

$(document).ready(function(){
    $("#text").click(function(){
    alert("Clicked");
    }); 
});

homepage.html:

<div>
    <script type="text/javascript" src="{% static "js/jquery.js" %}" ></script>
    <script type="text/javascript" src="{% static "js/test.js" %}" ></script>
    <p id="text"> Click to hide </p>     
</div>

in test2.js in line 2 i also tried: $('#text') and $("text") and $('text') ...

解决方案

In case it is a result of delayed event binding, try using jQuery's event delegation via the on() function. This would transform your existing

$(document).ready(function(){

  $("#text").click(function(){
    alert("Text Clicked");
  });

});

into

$(document).ready(function(){

  $("#text").on("click", function() {
    alert("Text Clicked");
  });

});

In the meantime, I would suggest that you modify the template to have the <script> tags within the <head> tags a la

<!DOCTYPE html>
<html>

  <head>

    <title>APP TITLE</title>
    <link href = "/path/to/css" rel = "stylesheet" >
    <script type = "text/javascript" src = "{% static "js/jquery.js" %}"></script>

  </head>

  <body>
    <p id = "text">Click To Hide</p>
  </body>

</html>

这篇关于Django JQuery项目不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆