我有一个名为id“tblCol1”的列的表。我怎样才能使用jQuery来添加HTML一个href标签呢?它的内容是什么?
谢谢!
答案 0 :(得分:1)
我认为您的表格中有一个<thead>
,其中一个<th>
具有该ID。
如果是这种情况,你会这样做:
// Get column index
var col = $("#tblCol1").index();
// Wrap each table cell with that index
$("table tbody tr").find("td:eq(" + col + ")").each(function () {
$(this).html("<a>" + $(this).html() + "</a>");
});
如果没有,并且您的意思是每行都有一个带有该ID的<td>
,那么......您不应该复制ID。将它们更改为类,并使用:
$("table td.tblCol1").each(function () {
$(this).html("<a>" + $(this).html() + "</a>");
});
答案 1 :(得分:0)
html()
方法可以设置并返回元素中的HTML。
$('tblCol1').html("<a>" + $('tblCol1').html() + "</a>");
如果单元格中没有HTML(只是文本),您可以使用:
$('tblCol1').html("<a>" + $('tblCol1').text() + "</a>");
答案 2 :(得分:0)
请参阅wrap()函数。
$mycol.wrap(jQuery('a').attr('href','http://somwhere.com'));
答案 3 :(得分:0)
关于列的ID的澄清点。
要为整列提供ID,您必须使用<colgroup>
标记以及<col>
标记;如上所述,您无法使用<thead>
标记执行此操作。
例如,如果您想为表格中的第一列提供ID,您可以编写类似这样的代码(<colgroup>
必须紧跟在<table>
代码之后):
<colgroup span="1" id="column1"></colgroup>
要分配包含表格中前3列的ID,它将如下所示:
<colgroup span="3" id="columngroup1"></colgroup>
要为表格中的每列分配一个ID,您必须使用<col>
标记,如下所示:
<colgroup>
<col id="col1" />
<col id="col2" />
</colgroup>
有关详细信息,请参阅http://www.w3.org/TR/html5/tabular-data.html#the-colgroup-element和http://www.w3schools.com/tags/tag_colgroup.asp