使用JS突出表格单元 - Firefox与IE [英] Highlighting Table Cells with JS - Firefox vs. IE

查看:52
本文介绍了使用JS突出表格单元 - Firefox与IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是JS的新手。希望我的问题的答案并非微不足道。


我写了一个简单的网页,提供了一个表格。 JS函数

允许查看者单击一个单元格,每个具有相同

内容的单元格的背景颜色变为浅绿色。

之前突出显示的每个单元格的背景颜色已更改回

white。这适用于Firefox,但与

IE无法正常工作。在IE中,之前突出显示的单元格都没有背景

颜色变回白色。如何修改语法以便它可以在两个浏览器中使用?
?下面是JS函数。一个简单的

网页将证明这个问题是在

http://uia.dyndns.org/test.html


感谢您的帮助。还要感谢Martin Honnen!他的帖子显示了

我如何做到这一点。


Lance


< script type =" text / javascript">

//遍历

文件中所有表格中所有单元格的脚本

function highlight_cell(元素){

var allTables = document.getElementsByTagName(''TABLE'');

var save_cell_contents = element.innerHTML

for(var i = 0; i< allTables.length; i ++){

table = allTables [i]

for(var row_index = 0; row_index< table.rows.length ;

row_index ++){

for(var c = 0; c< table.rows [row_index] .cells.length; c ++){


if(table.rows [row_index] .cells [c] .innerHTML == save_cell_contents){

table.rows [row_index] .cells [c] .bgColor ='' lime'';

}

else

if(table.rows [row_index] .cells [c] .bgColor ==''lime '')

table.rows [row_index] .cells [c] .bgColor =''white'';

}

}

}

}

< / script>

解决方案

lance写道:

将展示问题的网页是

< a rel =nofollowhref =http://uia.dyndns.org/test.htmltarget =_ blank> http://uia.dyndns.org/test.html

尝试更正所有内容:

< td bgcolor ="白色"

by

< td bgcolor =" white"


也许更喜欢css而不是html着色


< style type =" text / css">

td {background-color:white; }

< / style>

< script type =" text / javascript">
//用于遍历所有单元格的脚本
文件中的表格
函数highlight_cell(element){
var allTables = document.getElementsByTagName(''TABLE'');
var save_cell_contents = element.innerHTML
for(var i = 0; i< allTables.length; i ++){
table = allTables [i]
for(var row_index = 0; row_index< table.rows.length;
row_index ++){
for(var j = 0; j< table.rows [row_index] .cells.length; j ++){


//并使用css ?

var c = table.rows [row_index] .cells [j];

c.style.background = c.innerHTML == save_cell_contents? ''lime'':

c.style.background ==''lime''? ''white'':'''';

}
}
}
}
< / script>




在FF和IE5.1 IE5.2上成功测试(新)Mac

这里:
http://perso.wanadoo.fr/stephane.mmo...ight_cells.htm

-

Stephane Moriaux et son [moins] vieux Mac


2005年7月4日星期一21:07, lance(ra ************ @ comcast.net)在

留言中写道< 11 **************** ******【g47g2000cwa.googlegroups .com>



我是一名JS新手。希望我的问题的答案并非微不足道。

我写了一个简单的网页,提供了一个表格。 JS功能
允许观众点击一个单元格,每个具有相同内容的单元格的背景颜色变为浅绿色。之前突出显示的每个单元格的背景颜色都变回了白色。这适用于Firefox,但与IE无法正常工作。在IE中,以前没有一个突出显示的单元格背景颜色变回白色。




我正在做类似工作的地方我'我在日历上创建了什么。

这是HTML4.01-Strict,如果这有所不同。


显然, ''blahblahblah''被替换为你使用的任何方法

选择每个元素。

例如:document.getElementByID(''myID'')。style.backgroundCo lor ="#FF0000"


要突出显示我使用:

blahblahblah.style.backgroundColor ="#FF0000''


并删除我使用的高光:

blahblahblah.style.backgroundColor =""


我认为bgColor应该是style.backgroundColor但我可能错了。

我不是javascript专家,我只是反复敲击

键盘直到它工作。


PDannyD写道:


我认为bgColor应该是style.backgroundColor但我可能错了。
我不是javascript专家,我只是反复敲击
键盘直到它工作。




bgColor不是错误 - >这是关于FF的工作

但最好只用于文件


-

Stephane Moriaux et son [moins] vieux Mac


Hi,

I am a JS newbie. Hopefully the answer to my question is not trivial.

I have written a simple webpage that presents a table. A JS function
allows the viewer to click on a cell and each cell with the same
content has the background color changed to lime green. Each cell that
was previously highlighted has the background color changed back to
white. This works great with Firefox but does not work correctly with
IE. In IE, none of the previously highlighted cells has the background
color changed back to white. How can I modify the syntax so that it
will work in both browsers? Below is the JS function. A simple
webpage that will demonstrate the problem is at

http://uia.dyndns.org/test.html

Thanks for any help. Thanks also to Martin Honnen! His posts showed
me how this could be done.

Lance

<script type="text/javascript">
// Script for traversing all of the cells in all of the tables in the
document
function highlight_cell (element) {
var allTables = document.getElementsByTagName(''TABLE'');
var save_cell_contents=element.innerHTML
for (var i=0; i<allTables.length; i++){
table=allTables[i]
for (var row_index = 0; row_index < table.rows.length;
row_index++) {
for (var c=0; c < table.rows[row_index].cells.length; c++){

if(table.rows[row_index].cells[c].innerHTML==save_cell_contents){
table.rows[row_index].cells[c].bgColor=''lime'';
}
else
if(table.rows[row_index].cells[c].bgColor==''lime'')
table.rows[row_index].cells[c].bgColor=''white'';
}
}
}
}
</script>

解决方案

lance wrote:

webpage that will demonstrate the problem is at

http://uia.dyndns.org/test.html
Try to correct all your :
<td bgcolor=" white"
by
<td bgcolor="white"

And perhaps prefer css than html colorization

<style type="text/css">
td { background-color: white; }
</style>
<script type="text/javascript">
// Script for traversing all of the cells in all of the tables in the
document
function highlight_cell (element) {
var allTables = document.getElementsByTagName(''TABLE'');
var save_cell_contents=element.innerHTML
for (var i=0; i<allTables.length; i++){
table=allTables[i]
for (var row_index = 0; row_index < table.rows.length;
row_index++) {
for (var j=0; j < table.rows[row_index].cells.length; j++){
// and using css ?
var c = table.rows[row_index].cells[j];
c.style.background = c.innerHTML==save_cell_contents? ''lime'':
c.style.background==''lime''? ''white'':'''';
}
}
}
}
</script>



tested with succes on FF and IE5.1 IE5.2 (new) Mac
here :
http://perso.wanadoo.fr/stephane.mmo...ight_cells.htm
--
Stephane Moriaux et son [moins] vieux Mac


On Monday 04 July 2005 21:07, lance(ra************@comcast.net) wrote in
message <11**********************@g47g2000cwa.googlegroups .com>

Hi,

I am a JS newbie. Hopefully the answer to my question is not trivial.

I have written a simple webpage that presents a table. A JS function
allows the viewer to click on a cell and each cell with the same
content has the background color changed to lime green. Each cell that
was previously highlighted has the background color changed back to
white. This works great with Firefox but does not work correctly with
IE. In IE, none of the previously highlighted cells has the background
color changed back to white.



I''m doing something similar at work where I''m creating a What''s On calendar.
It''s in HTML4.01-Strict, if that makes a difference.

Obviously, the ''blahblahblah'' is replaced with whatever method you use to
select each element.
eg: document.getElementByID(''myID'').style.backgroundCo lor="#FF0000"

To highlight I use:
blahblahblah.style.backgroundColor="#FF0000''

and to remove the higlighting I use:
blahblahblah.style.backgroundColor=""

I think "bgColor" should be "style.backgroundColor" but I could be wrong.
I''m no javascript expert, I just hit my head repeatedly against the
keyboard until it works.


PDannyD wrote:


I think "bgColor" should be "style.backgroundColor" but I could be wrong.
I''m no javascript expert, I just hit my head repeatedly against the
keyboard until it works.



bgColor is not an error -> that''s work on FF
but preferably used only with document

--
Stephane Moriaux et son [moins] vieux Mac


这篇关于使用JS突出表格单元 - Firefox与IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆