[英]Using Jquery how can I find a table cell containing an element with the name property
How can I find a table cell which contains an element with an input element with a name and apply an attribute to it. 我如何找到一个表格单元格,其中包含一个带有带有名称的输入元素的元素,并对其应用属性。 eg I have a table as below
例如我有一张桌子如下
<table>
<tr><td><input name="someName" /></td></tr>
<tr><td><input name="findThisElement"/></td></tr>
<tr><td><input name="someName" /></td></tr>
</table>
I need to apply border attribute to the td with input element with the name findThisElement
我需要将边界属性应用于名称为
findThisElement
输入元素的td
<table>
<tr><td><input name="someName" /></td></tr>
<tr><td style="border:1px solid red;"><input name="findThisElement"/></td></tr>
<tr><td><input name="someName" /></td></tr>
</table>
I guess I have to use the following statement to apply the css I require but I m struggling to find the td 我想我必须使用以下语句来应用所需的css,但我正在努力寻找td
$(td).attr('style', 'border:1px solid red');
$("table input[name='findThisElement']")
将该样式应用于该输入元素的父td,
$("table input[name='findThisElement']").parent().css('border','1px solid red');
$("table")
- this code will return all tables on page $("table")
-此代码将返回页面上的所有表
$("table td")
- this code will return all cells that are in table $("table td")
-此代码将返回表中的所有单元格
$(table td input")
- this code will return all inputs that are inside cell in table $(table td input")
-此代码将返回表中单元格内的所有输入
$("table td input[name='someName']")
- this code will return all inputs with name someName
that are inside cell in table $("table td input[name='someName']")
-此代码将返回表中单元格内所有名称为someName
输入
And answer for your question will be : $("table td input[name='someName']").closest('td').attr('style', 'border:1px solid red');
您的问题的答案将是:
$("table td input[name='someName']").closest('td').attr('style', 'border:1px solid red');
First of all, you need to know about attribute selector . 首先,您需要了解属性选择器 。
With that in mind, you have 2 options. 考虑到这一点,您有2个选择。 You can you in a single query the
:has
and directly apply your css right after : 您可以在一个查询中使用
:has
并在之后直接直接应用CSS:
$("td:has(input[name=findThisElement])").css('border','1px solid red');
Or you can directly go to the <input>
and select the .closest()
<td>
: 或者,您可以直接转到
<input>
并选择.closest()
<td>
:
$("td input[name=findThisElement").closest('td').css('border','1px solid red');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.