jQuery .click()不工作 [英] jQuery .click() doesn't work
本文介绍了jQuery .click()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码的外部 *。js
档案:
I have external *.js
file with this code:
$(".hlavni_tema").click(function() {
alert("ok");
});
alert("loaded");
和HTML页面,其中包含:
and HTML page, that contains this:
<div id="tema">
<span id="hlavni_tema_1" class="hlavni_tema">Základní fyzikální pojmy a jednotky</span>
<input type="checkbox" name="tema" id="tema_1a" value="'1a'">
<label for="tema_1a">Základní fyzikální pojmy, měření ve fyzice</label>
...
<span id="hlavni_tema_8" class="hlavni_tema">Astrofyzika</span>
<input type="checkbox" name="tema" id="tema_8d" value="'8d'">
<label for="tema_8d" class="posledni_label">Fyzikální obraz světa</label>
</div>
在头中,我有jQuery和外部文件源:
In head I have both jQuery and external file sources:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="/_jscripts/tema.js"></script>
当我打开该页面时, alert
loaded
,以便导入文件。但是当我用 classhlavni_tema
点击span时,没有任何反应。我也尝试过 .on(click,function())
或 .on(click,#tema ))
,但没有什么工作。感谢您的帮助
When I open that page, alert
alerts "loaded"
, so that file is imported. But when I click on span with class "hlavni_tema"
, nothing happens. I've tried also .on("click", function())
or .on("click", "#tema", function())
, but nothing worked. Thanks for help
推荐答案
您必须在加载DOM后分配事件处理程序。
You have to assign event handlers after the DOM is loaded.
尝试此操作,它应该可以正常工作。
Try this, it should work.
$(document).ready(function(){
$(".hlavni_tema").click(function() {
alert("ok");
});
alert("Document loaded");
});
alert("Script loaded");
这篇关于jQuery .click()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文