简体   繁体   English

使用javascript将表格单元格中的文本转换为url

[英]Make text in table cell to urls using javascript

I have some tables and i am trying to make the contents in the table cell as urls if they start with http . 我有一些表,并且如果它们以http开头,我会尝试使表单元格中的内容作为url。 The code i have tried is as follows. 我尝试过的代码如下。 The issue is : it is becoming a hyperlink and not url.(for ex: if the table cell content is the text for https://www.google.com , the final link it is becoming is "https://myapplicationlink/www.google.com" . I am not able to figure out why. Please help. 问题是:它正在成为超链接而不是URL。(例如:如果表格单元格内容是https://www.google.com的文本,则它正在变成的最终链接是"https://myapplicationlink/www.google.com" 。我无法找出原因。请帮助。

 var tables=document.getElementsByTagName("TABLE");
 for (x=0;x<tables.length;x++)
{
 var rows=tables[x].getElementsByTagName('TR');
 for (i=0; i<rows.length; i++) 
  {
    var cells=rows[i].getElementsByTagName('td');
    for (z=0;z<cells.length;z++)
    {


 //code for styling etc which i removed

 var hyp;var c;
      c=cells[z].innerHTML;
     if (c.indexOf("http") >= 0) {
         hyp="<a href=" + cells[z].innerHTML  +"</a>";
      c=hyp;
   cells[z].innerHTML = c;

}


 }


  }
 }

Not sure why you would get the bug you're describing, but you're not closing your href tag in your code... 不知道为什么会得到您要描述的错误,但是您没有在代码中关闭href标签...

hyp="<a href=" + cells[z].innerHTML  +"</a>";

should be 应该

hyp="<a href=\"" + cells[z].innerHTML  +"\">some text here</a>";

Also, rather than drawing href into the cell you can convert the cell to be clickable like this 另外,您可以像这样将单元格转换为可点击的位置,而不是将href绘制到单元格中

 var hyp;var c;
  c=cells[z].innerHTML;
 if (c.indexOf("http") >= 0)          
   cells[z].onclick = (function(url){self.location.href = url})(c);

Well, you're inserting invalid HTML for one. 好吧,您要为其中一个插入无效的HTML。 This line: 这行:

hyp="<a href=" + cells[z].innerHTML  +"</a>";

Should actually be: 实际上应该是:

hyp='<a href="' + cells[z].innerHTML  + '">' + cells[z].innerHTML + '</a>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javascript将文本字段插入表格单元格 - inserting text-field to table cell using javascript 如何使用 javascript 使表格中的空白单元格可单击并使其在另一个表格上显示某些内容? - How do I make a blank empty cell in a table clickable and make it display something on another table using javascript? 将文本设置为 javascript 中的默认表格单元格文本 - Setting a text as default table cell text in javascript 使用 Javascript 制作干净的表单提交的 url - Make clean form submitted urls using Javascript 通过单击 Javascript 中的按钮在表格单元格中设置文本 - Set text in table cell by clicking a button in Javascript jQuery或javascript:截断表格单元格中的文本 - jquery or javascript: Truncate text in table cell 我可以使用JavaScript / AngularJS使表格单元格中的数字基于其值具有不同的颜色 - Can I make a number in a table cell have a different color based on its value using JavaScript/AngularJS 从html表格单元格获取内容值,并使用javascript将其显示在文本框中 - Get content value from a html table cell and make it appear to the textbox using javascript 使用 JavaScript 检测文本中的 URL - Detect URLs in text with JavaScript 使用javascript更改文本值以及HTML页面中表格中单元格的背景色 - Using javascript to change the value of the text, and the background color of a cell within a table in HTML page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM