繁体   English   中英

在firefox中,如何使用javascript更改treecell中文本的颜色

[英]In firefox, how to change color of text in a treecell using javascript

背景:我有以下XUL片段

<tree id="treeToChange" flex="1">
  <treecols>
    <treecol label = "First Column" id="c1" flex="1"/>
    <treecol label = "Second Column" id="c2" flex="1"/>
  </treecols>
  <treechildren>
    <treeitem>
      <treerow>
        <treecell label="Data for Column 1"/>
        <treecell label="Data for Column 2"/>
      </treerow>
    </treeitem>
  </treechildren>
</tree>

和以下css片段

tree {  font-size: 120%; color: green;}

这使我的列数据以绿色文本显示。

我在XUL页面上有很多这样的树对象

问题:在firefox中,响应单击事件,该事件调用了javascript例程,如何在第1列的红色中设置对象“ treeToChange”的数据,在蓝色的列中设置数据?

DOM元素的style属性包含该元素的所有CSS声明。 命名方案略有不同(camelCaps而不是破折号),但其他方面完全相同。

 element.style.color = 'blue';

您可以在Mozilla javascript手册中阅读有关style属性的更多信息。

要设置任何元素的颜色,可以使用:

function changecolour(elid, nc) {
    document.getElementById(elid).style.color = nc;
}

所以

changecolour("c1", "red");
var x = document.getElementsByClassName("cell");
for ( var i = 0; i < x.length; i ++ ) {
    changecolour(x, "blue");
}

如果将单元格类添加到单元格中,将更改数据的颜色(以避免与文档中的其他树冲突)

顺便说一句, 是document.getElementsByClassName:

function getElementsByClassName(className, tag, elm){
var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
var tag = tag || "*";
var elm = elm || document;
var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
var returnElements = [];
var current;
var length = elements.length;
for(var i=0; i<length; i++){
    current = elements[i];
    if(testClass.test(current.className)){
        returnElements.push(current);
    }
}
return returnElements;

}

事实证明element.style.color仅影响列标题,而在firefox中,只有通过对dataview进行编码才能影响树结构中的单元格。

代码片段如下:

 // DatabaseTreeView: Create a custom nsITreeView DatabaseTreeView: function(aTableData, aColumns) { this.getCellProperties = function(row,col,props){ var aserv=Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService); props.AppendElement(aserv.getAtom("color_"+col.id)); props.AppendElement(aserv.getAtom("font_"+col.id)); }; ... and modify the css as follows treechildren::-moz-tree-cell-text(color_c1){ color:DarkGreen} treechildren::-moz-tree-cell-text(color_c2){ color:Navy} treechildren::-moz-tree-cell-text(font_c1){ font-size:120%} treechildren::-moz-tree-cell-text(font_c1){ font-size:150%} 

// DatabaseTreeView: Create a custom nsITreeView DatabaseTreeView: function(aTableData, aColumns) { this.getCellProperties = function(row,col,props){ var aserv=Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService); props.AppendElement(aserv.getAtom("color_"+col.id)); props.AppendElement(aserv.getAtom("font_"+col.id)); }; ... and modify the css as follows treechildren::-moz-tree-cell-text(color_c1){ color:DarkGreen} treechildren::-moz-tree-cell-text(color_c2){ color:Navy} treechildren::-moz-tree-cell-text(font_c1){ font-size:120%} treechildren::-moz-tree-cell-text(font_c1){ font-size:150%}

// DatabaseTreeView: Create a custom nsITreeView DatabaseTreeView: function(aTableData, aColumns) { this.getCellProperties = function(row,col,props){ var aserv=Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService); props.AppendElement(aserv.getAtom("color_"+col.id)); props.AppendElement(aserv.getAtom("font_"+col.id)); }; ... and modify the css as follows treechildren::-moz-tree-cell-text(color_c1){ color:DarkGreen} treechildren::-moz-tree-cell-text(color_c2){ color:Navy} treechildren::-moz-tree-cell-text(font_c1){ font-size:120%} treechildren::-moz-tree-cell-text(font_c1){ font-size:150%}

 // DatabaseTreeView: Create a custom nsITreeView DatabaseTreeView: function(aTableData, aColumns) { this.getCellProperties = function(row,col,props){ var aserv=Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService); props.AppendElement(aserv.getAtom("color_"+col.id)); props.AppendElement(aserv.getAtom("font_"+col.id)); }; ... and modify the css as follows treechildren::-moz-tree-cell-text(color_c1){ color:DarkGreen} treechildren::-moz-tree-cell-text(color_c2){ color:Navy} treechildren::-moz-tree-cell-text(font_c1){ font-size:120%} treechildren::-moz-tree-cell-text(font_c1){ font-size:150%} 

我希望这对以后的人有帮助

暂无
暂无

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

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