3 回答
TA贡献2019条经验 获得超9个赞
$("#" + rowId).find("td").css("border-top", "1px solid #7f7f7f");
$('#' + rowId)
获取tr
元素。.find("td")
获取td
嵌套在您的元素中的所有元素tr
。.css("border-top", "1px solid #7f7f7f")
将相关样式应用于您的td
元素。
TA贡献1848条经验 获得超10个赞
除非您"tr"必须过滤css.
如果您this考虑$.each. 目前,它被应用于所有"td"元素。
$('#target td').css('border-top', '1px solid #7f7f7f');
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
text-align: left;
padding: 8px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr id="target">
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
this根据上下文考虑您的方法
$('#target').each(function() {
$("td", this).css('border-top', '1px solid #7f7f7f');
});
//OR using just `this` in the callback and selecting `td` elements in selector
/*$('#target td').each(function() {
$(this).css('border-top', '1px solid #7f7f7f');
});*/
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,
th {
text-align: left;
padding: 8px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr id="target">
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
TA贡献1817条经验 获得超6个赞
你有想念' td' $('#' + rowId)将是$('#' + rowId+ ' td')
$('#' + rowId+ ' td').each(function() {
$('td').css('border-top', '1px solid #7f7f7f');
});
添加回答
举报