如何使用jquery更改onclick事件中的特定元素
假设我有一个带有单个id和每个td的表,当有人使用jquery为这个特定的td单击任何td元素时,我想添加一些效果 例如:如何使用jquery更改onclick事件中的特定元素,jquery,Jquery,假设我有一个带有单个id和每个td的表,当有人使用jquery为这个特定的td单击任何td元素时,我想添加一些效果 例如: <table> <tr> <td id="11"> some text 1 </td> <td id="12"> some text 2 </td> <td id="13"> some text 3 </td> </tr> <
<table>
<tr>
<td id="11"> some text 1 </td>
<td id="12"> some text 2 </td>
<td id="13"> some text 3 </td>
</tr>
<tr>
<td id="21"> some text 4 </td>
<td id="22"> some text 5 </td>
<td id="23"> some text 6 </td>
</tr>
</table>
一些文本1
一些文本2
一些文本3
一些文本4
一些文本5
一些文本6
现在,当任何人单击td中的任何一个时,例如,id等于22的td,我想添加一些效果,例如(添加背景色/颜色等)
我该怎么做?提前谢谢
$('#22').on('click', function(){
$(this).css('background-color', '#f6ff00');
});
或作为对象(如果要更改多个属性):
建议:不要以数字开头您的id
$("td").click(function(){
$(this).css("background-color", "Red");
});
或仅针对特定td
$("#22").click(function(){
$(this).css("background-color", "Red");
});
更新:我猜值
是指td
中的文本吗?如果是这样的话,你可以用这个
$(this).text();
或
对于每个id
的不同效果,您可以执行以下操作:
JQuery
$("td").on('click', function(){
var tdid = $(this).attr('id'); //get the id of the td
switch (tdid){
case "11":
$(this).css("background-color", "Red");
break;
case "12":
$(this).css("background-color", "Green");
break;
case "13":
$(this).css("background-color", "blue");
break;
//... add the others here
case "22":
$(this).css("background-color", "orange");
break;
default:
$(this).css("background-color", "yellow");
break;
}
});
HTML
<table>
<tr>
<td id="11"> some text 1 </td>
<td id="12"> some text 2 </td>
<td id="13"> some text 3 </td>
</tr>
<tr>
<td id="21"> some text 4 </td>
<td id="22"> some text 5 </td>
<td id="23"> some text 6 </td>
</tr>
</table>
一些文本1
一些文本2
一些文本3
一些文本4
一些文本5
一些文本6
如何获取td的价值帮助检测吸血鬼。另外,
元素没有值。实际上,这个td的Id是从服务器动态生成的,我如何获得td的值$(此).val()未工作
$("#id").text();
$("td").on('click', function(){
var tdid = $(this).attr('id'); //get the id of the td
switch (tdid){
case "11":
$(this).css("background-color", "Red");
break;
case "12":
$(this).css("background-color", "Green");
break;
case "13":
$(this).css("background-color", "blue");
break;
//... add the others here
case "22":
$(this).css("background-color", "orange");
break;
default:
$(this).css("background-color", "yellow");
break;
}
});
<table>
<tr>
<td id="11"> some text 1 </td>
<td id="12"> some text 2 </td>
<td id="13"> some text 3 </td>
</tr>
<tr>
<td id="21"> some text 4 </td>
<td id="22"> some text 5 </td>
<td id="23"> some text 6 </td>
</tr>
</table>