[英]Adding Attribute to HTML element with jQuery
Utilizing jQuery .children... 正在使用jQuery .children ...
var strHtml = $(dt.row(row_idx).child()).children().children().html();
...to pass in the following HTML: ...以传递以下HTML:
<tbody>
<tr>
<td>Notes:</td>
<td>some info</td>
</tr>
<tr>
<td>Assistance Required:</td>
<td>Translation, writing and speaking capabilities </td>
</tr>
<tr>
<td>Unit:</td>
<td>not listed</td>
</tr>
</tbody>
How do I grab the label cells ( <td>Notes:</td>
, <td>Assistance Required:</td>
, <td>Unit:</td>
) and move to their next corresponding value cell to add a colspan property to the <td>
and then return the finished html? 如何获取标签单元格(
<td>Notes:</td>
, <td>Assistance Required:</td>
, <td>Unit:</td>
)并移至其下一个对应的值单元格以添加一个colspan属性为<td>
,然后返回完成的html?
The result should look like... 结果应该像...
<tbody>
<tr>
<td>Notes:</td>
<td colspan="12">some info</td>
</tr>
<tr>
<td>Assistance Required:</td>
<td colspan="12">Translation, writing and speaking capabilities </td>
</tr>
<tr>
<td>Unit:</td>
<td colspan="12">not listed</td>
</tr>
</tbody>
Take your string and load up a temporary element you create in memory only with that string. 取出您的字符串并加载仅使用该字符串在内存中创建的临时元素。
You can use the :nth-child()
pseudo-class selector to just select the second child <td>
element in each row and then use the JQuery .attr()
method to add an attribute to those elements: 您可以使用
:nth-child()
伪类选择器在每行中选择第二个子元素<td>
,然后使用JQuery .attr()
方法向这些元素添加属性:
var strHTML = `<table> <tbody> <tr> <td>Notes:</td> <td>some info</td> </tr> <tr> <td>Assistance Required:</td> <td>Translation, writing and speaking capabilities </td> </tr> <tr> <td>Unit:</td> <td>not listed</td> </tr> </tbody> </table>` // Create a temporary element in memory only var temp = document.createElement("div"); // Load the string into the temp element $(temp).html(strHTML); // Now, you can search through the element $("td:nth-child(2)", temp).attr("colspan", "12"); // Add the attribtue to the second td in each tr console.log($(temp).html()); // Get the HTML contents of the <table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.