Django JQuery项目不工作 [英] Django JQuery items dont work
问题描述
(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屋!