编写一个 jQuery 函数来测试 <td>
的类并采取相应行动。该功能通过单击img
来工作。里面<td>
并使用 td
的类信息进行测试。
$(document).ready(function () {
$('td img').click(function () {
if ((this).parent().hasClass('x')) {
alert("Seat " + ((this).parent().attr("id")) + " is taken");
} else if ((this).parent().hasClass('selected')){
$(this).attr('src', 'images/a.gif');
$(this).parent().removeClass('selected');
alert($(this).parent().attr("class"));
return false;
} else {
$(this).attr('src', 'images/c.gif');
$(this).parent().addClass('selected');
alert($(this).parent().attr("class"));
return false;
};
});
});
该错误表明图像的方法不存在,尽管我指定了父级。
最佳答案
缺少 $
符号。
If($(this).parent()...
这里发生的情况是,click 函数传递 dom/html 对象而不是 jQuery 对象,因此您必须使用 jQuery 函数构建该对象才能使用正确的对象!
关于javascript - jQuery 表格图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15192037/