jQuery:选择包含给定元素的HTML表

问题描述:

假设我有以下HTML

Let's say I have the following HTML

<table id = "mytable">
  <tr>
    <th>Hiring Manager</th>
    <td><textarea id = "textComments" cols = "15"/></td>
  </tr>
</table>

在上面的标记中,我想添加以下行

From the above markup, I'd like to append the following row

<tr>
   <td></td>
   <td>
       <span class = "CharCounter">
           <span id="lblCharCounter">Label</span>
           characters (500 max)
       </span>
   </td>
</tr>

我希望能够从文本区域中选择表格.

I want to be able to select the table, from the textarea.

感谢您的帮助

如果要在表中添加另一行,请找到textarea,然后慢慢进行操作,直至达到所需的目的.要返回一个元素,只需再次使用.parent().

If you want to add another row inside of the table, find the textarea, and slowly work your way until you get to the point you want. To go back an element, simply use .parent() again.

$('#textComments').parent().parent().after('
    <tr>
       <td></td>
       <td>
         <span class = "CharCounter">
           <span id="lblCharCounter">Label</span>
           characters (500 max)
         </span>
      </td>
   </tr>'
);