我试图在单击时将表格的行加粗。 我该怎么做才能让脚本知道我点击了哪一行? 我能想到的唯一方法是使用如下函数。
<script>
function bold(x){
$("#"+x).click(function(e){
$(this).css("font-weight","bold");
e.stopPropagation();
});
}
</script>
<table>
<tr id = 1 onclick = bold(this.id)><td>A</td></tr>
<tr id = 2 onclick = bold(this.id)><td>B</td></tr>
<tr id = 3 onclick = bold(this.id)><td>C</td></tr>
<tr id = 4 onclick = bold(this.id)><td>D</td></tr>
<tr id = 5 onclick = bold(this.id)><td>E</td></tr>
<tr id = 6 onclick = bold(this.id)><td>F</td></tr>
</table>
有没有更好的方法可以不使用函数来做到这一点?
最佳答案
你可以试试这个
$('tr').click(function(e){
$(this).css("font-weight","bold");
e.stopPropagation();
});
此外,您还将您的 id 括在引号中。首先执行此操作
<tr id = "1" onclick = bold(this.id)><td>A</td></t
r>
根据 HTML5 标准,没有必要用引号引起来..但我认为这样做是一个很好的做法..
关于jquery - 使用 Jquery 粗体表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12701017/