I am using the following code to add columns dynamically to a html table:
我使用以下代码动态地将列添加到html表:
var tblHeadObj = window.opener.document.getElementById("maintable").tHead;
var j=0;
while(j < fname.length)
{
if(tblHeadObj != null)
{
for(var h = 0; h < tblHeadObj.rows.length; h++)
{
var newTH = window.opener.document.createElement('th');
tblHeadObj.rows[h].appendChild(newTH);
//newTH.innerHTML='[th]row:'+h+'cell:'+(tblHeadObj.rows[h].cells.length-1)
}
}
var tblBodyObj = window.opener.document.getElementById("maintable").tBodies[0];
//for(var i = 0; i < tblBodyObj.rows.length; i++) {
var newCell=tblBodyObj.rows[0].insertCell(-1);
var newCell=tblBodyObj.rows[0].insertCell(-1);
// newCell.innerHTML = (tblBodyObj.rows[0].cells.length - 1)
newCell.innerHTML= fname[j];
j++;
}
Now i want to make columns as link.How can i do that?
现在我想把列作为链接。我怎么能这样做?
Thanks
3 个解决方案
#1
As others have noted, it is quite unclear what you mean by "make columns as link". However, we as a community have become accustomed to making guesses about the real problem and providing a solution based on that assumption. As we gain experience tackling more and more unclear questions, our ESP skill become more honed.
正如其他人所指出的那样,“将列作为链接”是什么意思还不清楚。然而,作为一个社区,我们已经习惯于对真实问题进行猜测并提供基于该假设的解决方案。随着我们获得越来越多不清楚问题的经验,我们的ESP技能变得更加磨练。
It appears that you are creating an HTML table via DOM methods. I will assume that you want to create a link within the created tablecell and here is my suggestion:
您似乎正在通过DOM方法创建HTML表。我假设你想在创建的tablecell中创建一个链接,这是我的建议:
Use the same createElement
method to create any elements you need. For instance, a link (anchor) can be created with the following code:
使用相同的createElement方法创建所需的任何元素。例如,可以使用以下代码创建链接(锚点):
var link = document.createElement("a");
link.setAttribute("href", "http://www.microsoft.com")
link.className = "someCSSclass";
// For IE only, you can simply set the innerText of the node.
// The below code, however, should work on all browsers.
var linkText = document.createTextNode("Click me");
link.appendChild(linkText);
// Add the link to the previously created TableCell.
newCell.appendChild(link);
Alternatively, you can also set the innerHTML of the TableCell as @Anonymous has suggested.
或者,您也可以像@Anonymous所建议的那样设置TableCell的innerHTML。
#2
If you're trying to put the cell contents into an anchor, then one way is to change
如果您正在尝试将单元格内容放入锚点,那么一种方法是更改
newCell.innerHTML= fname[j];
to
newCell.innerHTML= '<a href="'+whatever+'">'+fname[j]+'</a>';
where whatever
is a variable holding an appropriate string.
无论什么是包含适当字符串的变量。
Beware that the contents of fname[j]
are all inline (eg, not tables or blocks like div, headings, forms -- but form inputs are okay) or the anchor will be closed by most browsers prematurely. If need be, you could put the anchor only around parts of the cell's contents, but the easiest way to do that would depend on what the contents are.
请注意fname [j]的内容都是内联的(例如,不是表格或块,如div,标题,表单 - 但表单输入是可以的),或者大多数浏览器过早地关闭锚点。如果需要,你可以只将锚点放在单元格内容的一部分上,但最简单的方法取决于内容是什么。
#3
This are all good but I needed an image in the link so here is that code:
这一切都很好,但我需要链接中的图像,所以这里是代码:
cell[k] = document.createElement('td');
var link = document.createElement('a');
link.setAttribute('href', "http://www.ilovethismusic.com");
link.setAttribute('target', "_blank");
var newimg = document.createElement('img');
newimg.src = "http://www.ilovethismusic.com/Views/Images/bg_header.jpg";
newimg.alt = "imageMissing";
newimg.width = "95";
newimg.height = "45";
newimg.border = "0";
link.appendChild(newimg);
cell[k].appendChild(link);
#1
As others have noted, it is quite unclear what you mean by "make columns as link". However, we as a community have become accustomed to making guesses about the real problem and providing a solution based on that assumption. As we gain experience tackling more and more unclear questions, our ESP skill become more honed.
正如其他人所指出的那样,“将列作为链接”是什么意思还不清楚。然而,作为一个社区,我们已经习惯于对真实问题进行猜测并提供基于该假设的解决方案。随着我们获得越来越多不清楚问题的经验,我们的ESP技能变得更加磨练。
It appears that you are creating an HTML table via DOM methods. I will assume that you want to create a link within the created tablecell and here is my suggestion:
您似乎正在通过DOM方法创建HTML表。我假设你想在创建的tablecell中创建一个链接,这是我的建议:
Use the same createElement
method to create any elements you need. For instance, a link (anchor) can be created with the following code:
使用相同的createElement方法创建所需的任何元素。例如,可以使用以下代码创建链接(锚点):
var link = document.createElement("a");
link.setAttribute("href", "http://www.microsoft.com")
link.className = "someCSSclass";
// For IE only, you can simply set the innerText of the node.
// The below code, however, should work on all browsers.
var linkText = document.createTextNode("Click me");
link.appendChild(linkText);
// Add the link to the previously created TableCell.
newCell.appendChild(link);
Alternatively, you can also set the innerHTML of the TableCell as @Anonymous has suggested.
或者,您也可以像@Anonymous所建议的那样设置TableCell的innerHTML。
#2
If you're trying to put the cell contents into an anchor, then one way is to change
如果您正在尝试将单元格内容放入锚点,那么一种方法是更改
newCell.innerHTML= fname[j];
to
newCell.innerHTML= '<a href="'+whatever+'">'+fname[j]+'</a>';
where whatever
is a variable holding an appropriate string.
无论什么是包含适当字符串的变量。
Beware that the contents of fname[j]
are all inline (eg, not tables or blocks like div, headings, forms -- but form inputs are okay) or the anchor will be closed by most browsers prematurely. If need be, you could put the anchor only around parts of the cell's contents, but the easiest way to do that would depend on what the contents are.
请注意fname [j]的内容都是内联的(例如,不是表格或块,如div,标题,表单 - 但表单输入是可以的),或者大多数浏览器过早地关闭锚点。如果需要,你可以只将锚点放在单元格内容的一部分上,但最简单的方法取决于内容是什么。
#3
This are all good but I needed an image in the link so here is that code:
这一切都很好,但我需要链接中的图像,所以这里是代码:
cell[k] = document.createElement('td');
var link = document.createElement('a');
link.setAttribute('href', "http://www.ilovethismusic.com");
link.setAttribute('target', "_blank");
var newimg = document.createElement('img');
newimg.src = "http://www.ilovethismusic.com/Views/Images/bg_header.jpg";
newimg.alt = "imageMissing";
newimg.width = "95";
newimg.height = "45";
newimg.border = "0";
link.appendChild(newimg);
cell[k].appendChild(link);