简体   繁体   English

Tampermonkey,Chrome如何自动将行插入表格?

[英]Tampermonkey, Chrome how to automatically insert rows into a table?

I've tried with a stupid way of inserting code for a new table, but not even that seems to work. 我尝试了一种为新表插入代码的愚蠢方法,但似乎还行不通。 What would be the proper way? 正确的方法是什么? Here's what I tried to do: 这是我尝试做的事情:

var table = document.getElementsByClassName("test")
[0].getElementsByClassName("tableclass");
for (var i = 0, l = table.length; i < l; i++) {
  var content = table[i];
  let s = content.innerHTML;
  s = s.replace(/table/g, 'table border="1"');
  s = s.replace(/tr>[\s\S]*?<tr>[\s\S]*?<td>3/g, 'tr>\r\n<tr>\r\n<td>3');
  content.innerHTML = s;
}

And a fiddle: https://jsfiddle.net/d10tk7nr/1/ 和一个小提琴: https : //jsfiddle.net/d10tk7nr/1/

Also, the reason my stupid way doesn't contain the whole table is because some of the cells where I want to eventually use this would contain random data and I don't know how to skip that. 另外,我的愚蠢方法不包含整个表的原因是因为最终要使用此单元格的某些单元格将包含随机数据,而且我不知道如何跳过。

If you want to create a new HTML-Element, every browser got you covered on that. 如果您想创建一个新的HTML元素,那么每种浏览器都可以满足您的要求。

var tr = document.createElement('tr');
console.log(tr);

The browser console will show you exactly what you have created - a new HTML element that is not yet part of the DOM: 浏览器控制台将向您确切显示您所创建的内容-一个尚未包含在DOM中的新HTML元素:

<tr></tr>

The same goes with the creation of some content for that table row: 为该表行创建一些内容也是如此:

var td1 = document.createElement('td'),
    td2 = document.createElement('td');
td1.innerText = '5';
td2.innerText = '6';
console.log(td1, td2);

The result will be two td-elements: 结果将是两个td元素:

<td>5</td>   <td>6</td>

Now we have to glue these parts together. 现在我们必须将这些部分粘合在一起。 Browsers will also have you coverd on this: 浏览器还将使您对此感兴趣:

tr.append(td1);
tr.append(td2);
console.log(tr);

The result is a complete table row: 结果是一个完整的表行:

<tr><td>5</td><td>6</td></tr>

All we have to do is append this row to your table: 我们要做的就是将此行添加到表中:

var table = document.querySelector('.test table tbody');
table.append(tr);

The elements you have created are now part of the DOM - child elements of the body of your table to be excact. 现在,您创建的元素已成为DOM的一部分-要成为表主体的子元素。

Click here for a fiddle 单击此处拨弄小提琴

Edit 编辑

If you want to insert the new row to a specific place, you have to find the element you that should be next to it and use insertBefore . 如果要将新行插入到特定位置,则必须找到该行旁边的元素,并使用insertBefore This would change the the last piece of code to: 这会将最后一段代码更改为:

var targetTr = document.querySelector('.test table tr:nth-child(2)');
targetTr.parentNode.insertBefore(tr, targetTr);

If you want to choose where to put your new row within your javascript, you can use the childNodes property: 如果要选择将新行放在javascript中的位置,则可以使用childNodes属性:

console.log(table.childNodes);

I'd use insertAdjacentHTML, like so: 我将使用insertAdjacentHTML,如下所示:

table[i].insertAdjacentHTML('beforeend', '<tr><td>5</td><td>6</td></tr>');

Please see this fiddle: https://jsfiddle.net/52axLsfn/4/ 请看这个小提琴: https : //jsfiddle.net/52axLsfn/4/

Also demonstrates how to set the border. 还演示了如何设置边框。 Note that this code targets all tables, so depending on your situation you may want to be more specific. 请注意,此代码针对所有表,因此,根据您的情况,您可能需要更具体。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM