Delphi的 VCL类层次清晰强大,最近在写JavaScript 在想用VCL的类层次实现一个用JavaScript
写的封装页面控件。  基本实现了TABLE的封装,在界面上一下子简单明了了很多,所有要显示在TABLE上的数据都是XML格式,直接加载。  现在想如何才能将CSS动态的应用。
 JavaScript 其实功能也很强,就是调试不方便,一个大小写,格式不正确就得找半天。

解决方案 »

  1.   

    这是StdCtrls.js 里实现的对 HTML selected 的基本封装
    <!-- JCustomListBox-->

    function JCustomListBox(){
    //this.fObj=null;
    }

    JCustomListBox.prototype.setParent=function(value){
    this.fObj=value;
    }

    JCustomListBox.prototype.add=function (aText,aValue){
    this.fObj.options.add(new Option(aText,aValue));
    }

    JCustomListBox.prototype.getCount=function(){
    return this.fObj.options.length;
    }

    JCustomListBox.prototype.getSelectedIndex=function(){
    return this.fObj.options.selectedIndex;
    }

    JCustomListBox.prototype.setSelectedIndex=function(value){
    this.fObj.options.selectedIndex=value;
    }

    JCustomListBox.prototype.getValue=function(index){
    return this.fObj.options[index].value;
    }

    JCustomListBox.prototype.setValue=function(index,value){
    return this.fObj.options[index].value=value;
    }

    JCustomListBox.prototype.getText=function(index){
    return this.fObj.options[index].text;
    }

    JCustomListBox.prototype.setText=function(index,value){
    this.fObj.options[index].text=value;
    }

    JCustomListBox.prototype.remove=function(index){
    this.fObj.options.remove(index);

    return 1;
    }

    JCustomListBox.prototype.findValue=function(value){
    for(var i=0; i<this.getCount(); i++) {
    if(this.getValue(i)==value){
    return i;
    }
    }
    return -1;
    }

    JCustomListBox.prototype.findText=function(value){
    for(var i=0; i<this.getCount(); i++) {
    if(this.getText(i)==value){
    return i;
    }
    }
    return -1;
    }


    JCustomListBox.prototype.removeSelected=function(index){
    var resCount=0;
    for(var i=this.getCount()-1 ;i>=0;i--){
    if (this.getSelected(i))
    this.fObj.options.remove(i);
    resCount++;
    }
    return resCount;
    }

    JCustomListBox.prototype.LoadXMLNode=function(node){
    //<item id="">text</item>
    var item=null;
    var id="";
    for (var i=0;i<node.childNodes.length;i++){
    id="";
    for (var att=0;att<node.childNodes[i].attributes.length;att++)
    if (node.childNodes[i].attributes.item(att).nodeName=="id"){
    id=node.childNodes[i].attributes.item(att).text;
    break;
    }
    this.add(id,node.childNodes[i].text);
    }
    }

    JCustomListBox.prototype.clear=function(){
    for(var i=this.getCount()-1 ;i>=0;i--){
    this.remove(i);
    }

    }

    JCustomListBox.prototype.getSelected=function(index){
    if (this.fObj.options(index).selected)
    return true;
    else
    return false;
    }

    JCustomListBox.prototype.selectCount=function(){
    var resCount=0;
    for ( var i = 0; i < this.fObj.options.length; i++ ) {
    if (this.fObj.options(i).selected){
    resCount++;
    }
    }
    return resCount;
    } JCustomListBox.prototype.getHTML=function(){
    var resStr="<select multiple=\"multiple\" size=\"10\">\n";
    for ( var i = 0; i < this.fObj.options.length; i++ ) {
    resStr=resStr+"<option value=\""+this.getValue(i)+"\">"+this.getText(i)+"</option>\n";
    }
    resStr=resStr+"</select>";
    return resStr;
    }


    <!-- ComboBox-->

    function JComboBox(){
    }

    JComboBox.prototype=new JCustomListBox();

    <!-- ListBox-->

    function JListBox(){

    }

    JListBox.prototype=new JCustomListBox(); 测试
    <head>
    function test(){
    var listbox=new JListBox();
    listbox.setParent(ListBox1);
    listbox.add("测试1",1);
    listbox.add("测试2",2);
    listbox.add("测试3",3);
    alert(listbox.getCount());}
    </head>
    <Body>
    <select name="ListBox1" multiple="multiple" size="10" style="width:200px">
    <option>测试</option>
    </select>
    <input type="button" onclick="test()" value="测试"/></body>
      

  2.   

    最终的目标是实现创建HTML界面时就象DELPHI下创建界面一样,可以动态的指定事件。使生成的HTML界面简单清晰。消息可以用定时器实现。现在还没全部想清楚,大家给定建议。