Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript在html表中创建链接_Javascript_Html_Dom - Fatal编程技术网

使用javascript在html表中创建链接

使用javascript在html表中创建链接,javascript,html,dom,Javascript,Html,Dom,我正在使用以下代码向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 = wind

我正在使用以下代码向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++;
}
var tblHeadObj=window.opener.document.getElementById(“maintable”).tHead;
var j=0;
而(j
现在我想把列作为链接。我怎么做


谢谢

如果您试图将单元格内容放入锚定,那么一种方法就是更改

newCell.innerHTML=  fname[j];

newCell.innerHTML='';
其中
which
是一个包含适当字符串的变量


请注意,
fname[j]
的内容都是内联的(例如,不是像div、headers、forms这样的表或块,但表单输入是可以的)或者大多数浏览器会过早地关闭锚。如果需要,您可以只在单元格内容的部分内容周围放置锚,但最简单的方法取决于内容是什么。

正如其他人所指出的,您不清楚“将列作为链接”是什么意思。然而,作为一个社区,我们已经习惯于对真实问题进行猜测,并根据该假设提供解决方案。随着我们在处理越来越多不明确问题方面获得经验,我们的ESP技能也变得越来越成熟

您似乎正在通过DOM方法创建HTML表。我假设您希望在已创建的tablecell中创建链接,下面是我的建议:

使用相同的
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);

或者,您也可以将TableCell的innerHTML设置为@Anonymous所建议的格式。

这些都很好,但我需要在链接中添加一个图像,因此下面是代码:

  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);

请解释“将列作为链接”的含义。表列中的值应显示为链接,我可以重定向到soem位置。它不检索fname的值。它在输出中显示+fname[j]+,如果您的意思是脚本按字面意思输出“+fname[j]+”在文档中,则会出现引用错误。请仔细注意单引号的位置:它们会将字符串文字装订起来。如果锚的内容是纯文本,则这种方法不太容易出错;当出现错误时,您会收到很好的警告,而不是处理错误标记中的键入错误。
  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);