交换表列 [英] Swapping table columns
问题描述
我从以下某处交换表格列得到以下功能
互联网
函数swapColumns(table,colIndex1,colIndex2){
if(table&& table.rows&& table.insertBefore&& colIndex1!=
colIndex2){
for( var i = 0; i< table.rows.length; i ++){
var row = table.rows [i];
var cell1 = row.cells [ colIndex1];
var cell2 = row.cells [colIndex2];
var siblingCell1 = row.cells [Number(colIndex1)+ 1];
row.insertBefore(cell1,cell2);
row.insertBefore(cell2,siblingCell1);
}
}
}
其中" table"是一个Table对象,colIndex1和colIndex2是
整数,表示要交换的列号。
问题是......它什么都不做。 Firefox Javascript没有错误
控制台,但它不会做任何事情!
我使用squarefree.com的JS shell检查了一些基本的东西
就像rows.length一样,确保它进入循环等等。但
insertBefore似乎没有做任何事情。
帮助?谢谢
I got the following function to swap table columns from somewhere on
the Internet
function swapColumns (table, colIndex1, colIndex2) {
if (table && table.rows && table.insertBefore && colIndex1 !=
colIndex2) {
for (var i = 0; i < table.rows.length; i++) {
var row = table.rows[i];
var cell1 = row.cells[colIndex1];
var cell2 = row.cells[colIndex2];
var siblingCell1 = row.cells[Number(colIndex1) + 1];
row.insertBefore(cell1, cell2);
row.insertBefore(cell2, siblingCell1);
}
}
}
where "table" is a Table object and colIndex1 and colIndex2 are
integers representing the column numbers to swap.
Problem is...it doesnt do anything. No errors in Firefox Javascript
console, but it just doesnt do a thing!
I checked a few basic things using the JS shell from squarefree.com
like rows.length, made sure it is entering the loop, etc. But the
insertBefore doesnt seem to be doing anything.
Help? Thanks
推荐答案
VA写道:
VA wrote:
我有以下函数从互联网上的某个地方交换表格列
函数swapColumns(table,colIndex1,colIndex2){
if(table&& table.rows&& table.insertBefore&&& colIndex1!=
colIndex2){
for(var i = 0; i< table.rows.length; i ++){
var row = table.rows [i];
var cell1 = row.cells [colIndex1];
var cell2 = row.cells [colIndex2];
var siblingCell1 = row.cells [ Number(colIndex1)+ 1];
row.insertBefore(cell1,cell2);
row.insertBefore(cell2,siblingCell1);
}
}
}
I got the following function to swap table columns from somewhere on
the Internet
function swapColumns (table, colIndex1, colIndex2) {
if (table && table.rows && table.insertBefore && colIndex1 !=
colIndex2) {
for (var i = 0; i < table.rows.length; i++) {
var row = table.rows[i];
var cell1 = row.cells[colIndex1];
var cell2 = row.cells[colIndex2];
var siblingCell1 = row.cells[Number(colIndex1) + 1];
row.insertBefore(cell1, cell2);
row.insertBefore(cell2, siblingCell1);
}
}
}
是某个地方 FAQTs
< http://www.faqts.com/knowledge_base/view.phtml/aid/32355/fid/192> ;?
包含测试用例,这真的不适合你吗?然后
请提供一个代码不起作用的URL,确保它是一个带有表和脚本的测试用例,但不是一些有很多
与代码无关的东西。
-
Martin Honnen
http://JavaScript.FAQTs.com/
Martin:感谢您的回复,是的,确实是我从中获得了
函数,感谢您的写作。
当我将整个HTML文档保存到一个本地文件,打开它在
Firefox中,然后点击Swap Columns按钮,它工作正常。
但是当我在我自己的代码中使用它时它不起作用。
我确实确保将一个Table对象和2个有效列
索引传递给你的函数。我还用JS shell来打印
print(mytable.rows [0] .cells [0] .innerHTML)获得一堆下标
确保我是导航右表。它只是没有交换
列!
如何排除故障?
谢谢
Martin: Thanks for replying, yes that is indeed where I got the
function from, thanks for writing it.
When I save the entire HTML document to a local file, open it in
Firefox, and click on the Swap Columns button, it works fine.
But when I use it in my own code, it doesnt work.
I did make sure that I pass in a Table object and 2 valid column
indexes to your function. I also used the JS shell to do
print(mytable.rows[0].cells[0].innerHTML) for a bunch of subscripts to
make sure I was navigating the right table. It just doesnt swap the
columns!
How can I troubleshoot this?
Thanks
VA写道:
VA wrote:
但是当我在自己的代码中使用它时,它不会工作。
But when I use it in my own code, it doesnt work.
考虑在网上放一个样本并在这里发布URL,这样我们就可以检查出错了什么。
-
Martin Honnen
http://JavaScript.FAQTs.com/
这篇关于交换表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!