我使用jquery在li上绑定一个on click事件。当dom准备就绪时,我只是编写简单的jquery代码。但我不知道为什么它不起作用。我用chrome或firefox检查,并且没有js的控制台错误。我正在使用jQuery v1.11.1
的JavaScript
$(document).ready(function() {
console.log('dom ready');
$('#student_list_ul ul li').click(function(){
alert('li clicked');
});
});
HTML
<ul class="list-inline text-center" id="student_list_ul">
<li>
<img src="images/student.png" alt="girl_student_image" class="student">
<div class="studentname">MICHEL HUSSY</div>
</li>
<li>
<img src="images/girl_student.png" alt="student_image" class="student">
<div class="studentname">LUSY PARKAR</div>
</li>
<li>
<img src="images/student.png" alt="girl_student_image" class="student">
<div class="studentname">AB DEVILIERS</div>
</li>
</ul>
答案 0 :(得分:4)
从选择器中删除ul
。
应为$('#student_list_ul li')
答案 1 :(得分:0)
试试这个,
$('#student_list_ul li').click(function(){ // remove the ul from selector hierarchy
alert('li clicked');
});
或试试,
$('ul#student_list_ul li').click(function(){ // add ul before ul id
alert('li clicked');
});
$('#student_list_ul li').click(function() { // remove the ul from selector hierarchy
alert($(this).text()+' clicked!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="list-inline text-center" id="student_list_ul">
<li>
<img src="images/student.png" alt="girl_student_image" class="student">
<div class="studentname">MICHEL HUSSY</div>
</li>
<li>
<img src="images/girl_student.png" alt="student_image" class="student">
<div class="studentname">LUSY PARKAR</div>
</li>
<li>
<img src="images/student.png" alt="girl_student_image" class="student">
<div class="studentname">AB DEVILIERS</div>
</li>
</ul>
答案 2 :(得分:0)
JS代码:
$(document).ready(function() {
console.log('dom ready');
$('#student_list_ul li').click(function(){
alert('li clicked');
});
});
这会对你有所帮助
答案 3 :(得分:0)
Try this code
<ul class="list-inline text-center" id="student_list_ul">
<li>
<img src="images/student.png" alt="girl_student_image" class="student">
<div class="studentname">MICHEL HUSSY</div>
</li>
<li><img src="images/girl_student.png" alt="student_image" class="student">
<div class="studentname">LUSY PARKAR</div>
</li>
<li><img src="images/student.png" alt="girl_student_image" class="student">
<div class="studentname">AB DEVILIERS</div>
</li>
$(document).ready(function() {
console.log('dom ready');
$('#student_list_ul li').click(function(){
alert('li clicked');
});
});