<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 8 年前。
标签 javascript jquery html css
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 8 年前。
我动态地向表格添加背景图像,并有一个 .click 函数,该函数启用一个基本上使单元格变灰的类。
var url;
url = images[i];
newImage = "url("+url+")";
document.getElementById('sq'+(i+1)).style.backgroundImage = newImage;
}
$('body').on('click','td', function() {
alert("Clicked");
$(this).toggleClass('ui');
if ($(this).data('value') == 1) {
$(this).data('value', 0);
} else {
$(this).data('value', 1);
}
这是 css 类 'ui'
.ui {
visibility: hidden;
border: 3px solid black;
background-color: #ff1e72 !important;
color: #FFF !important;
}
这是表格html
<tr>
<td data-value="0" id="sq1"></td>
<td data-value="0" id="sq2"></td>
<td data-value="0" id="sq3"></td>
<td data-value="0" id="sq4"></td>
<td data-value="0" id="sq5"></td>
</tr>
最佳答案
你应该尝试这样的事情:
$(this).attr("data-value",thevalue);
$(function(){
$("td").click(function(){
alert("Clicked");
$(this).toggleClass('ui');
console.log($(this).data('value'));
if ($(this).data('value') == 1) {
$(this).attr("data-value",0);
} else {
$(this).attr("data-value",1);
}
});
});
关于javascript - 为什么我的 td onClick 没有触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26345999/