在Ajax中更新的div中显示CSS图像

时间:2009-03-11 11:33:04

标签: css image tree icons

我想知道是否有人可以提供帮助。我的页面中的HTML div包含树控件,该控件根据用户按下的按钮显示或隐藏。该按钮触发Ajax事件,该事件在服务器上设置一个变量以显示或隐藏树,以便状态保持不变。

但这是问题;当重新显示树时,不存在用于展开/折叠brance的图标。到目前为止,我还没有弄清楚为什么会这样。

树如下所示:第一个图形显示树应该是,第二个图形在隐藏并重新显示之后显示。

alt text http://www.dcs.bbk.ac.uk/~martin/Tree_with_icons.png alt text http://www.dcs.bbk.ac.uk/~martin/Tree_without_icons.png

树的HTML作为列表构建在服务器上,每个列表项都有一个CSS的类引用,如下所示:

ul.tree  li.liOpen    .bullet {
   background: url(myApp_Minus.png) center left no-repeat;
   cursor: pointer;
}
ul.tree  li.liClosed  .bullet {
   background: url(myApp_Plus.png) center left no-repeat;
   cursor: pointer;
}
ul.tree  li.liBullet  .bullet {
   background: url(myApp_Hyphen.png) center left no-repeat; 
   cursor: pointer;
}

有人可以建议在重新显示树时显示图标的方法吗?

我已尝试在div,内联CSS元素等中添加CSS文件的链接,但没有成功。

欢迎任何帮助。

我在运行时附加了树的HTML摘录:

<td align = "left">
        <div id = "tree"><ul class = "tree" id = "navTree">
        <li class = "liOpen">
          <a href = "/myDataSharer/aboutConcept#communities">
            <img alt = "Community" src = "/myDataSharer/images/myDataSharer_Community_Small.png">
          </a>&nbsp
          <a href = "/myDataSharer/commDashboard?CommNo=1&TagText=">Martin</a>
          <ul>
              <li class = "liOpen">
                <a href = "/myDataSharer/aboutConcept#datasets">
                  <img alt = "Tabular dataset" src = "/myDataSharer/images/myDataSharer_TabularDataset_Small.png">
                </a>&nbsp
                <a href = "/myDataSharer/displayDataset?DatasetNo=1&pageRowNo=0&TagText=">Planets</a>
              </li>
          <ul>
          <li>
            <a href = "/myDataSharer/aboutConcept#QAV">
               <img alt = "Visualisation" src = "/myDataSharer/images/myDataSharer_Visualisation_Small.png">
            </a>&nbsp
            <a href = "/myDataSharer/displayQAV?QAVNo=109&TagText=">Test QAV</a>
          </li>
          <li>
            <a href 

树本身位于一个名为“树”的div中,它从Javascript方法更新如下:

document.getElementById("tree").style.visibility = "visible";               
document.getElementById("tree").innerHTML = str;

树的Javascript是:

/*  WRITTEN BY: Martin O'Shea for myDataSharerAlpha.
 *
 *  This program has been inherited verbatim from the original author's sample code as mentioned
 *  below. No changes have been made other than a rename of a variable on line 121 from 'mktree' to 'tree'.

 *  ===================================================================
 *  Author: Matt Kruse <matt@mattkruse.com>
 *  WWW: http://www.mattkruse.com/
 *
 *  NOTICE: You may use this code for any purpose, commercial or
 *  private, without any further permission from the author. You may
 *  remove this notice from your final code if you wish, however it is
 *  appreciated by the author if at least my web site address is kept.
 *
 *  You may *NOT* re-distribute this code in any way except through its
 *  use. That means, you can include it in your product, or your web
 *  site, or any other form where the code is actually being used. You
 *  may not put the plain javascript up on your site for download or
 *  include it in your javascript libraries for download.
 *  If you wish to share this code with others, please just point them
 *  to the URL instead.
 *  Please DO NOT link directly to my .js files from your site. Copy
 *  the files to your server and use them there. Thank you.
 *  =====================================================================

 *  HISTORY
 *   ------------------------------------------------------------------
 *   December 9, 2003: Added script to the Javascript Toolbox
 *   December 10, 2003: Added the preProcessTrees variable to allow user
 *        to turn off automatic conversion of UL's onLoad
 *   March 1, 2004: Changed it so if a <li> has a class already attached
 *       to it, that class won't be erased when initialized. This allows
 *       you to set the state of the tree when painting the page simply
 *       by setting some <li>'s class name as being "liOpen" (see example)
 *
 *  This code is inspired by and extended from Stuart Langridge's aqlist code:
 *      http://www.kryogenix.org/code/browser/aqlists/
 *      Stuart Langridge, November 2002
 *      sil@kryogenix.org
 *      Inspired by Aaron's labels.js (http://youngpup.net/demos/labels/)
 *      and Dave Lindquist's menuDropDown.js (http://www.gazingus.org/dhtml/?id=109)
 */

// Automatically attach a listener to the window onload, to convert the trees
addEvent(window,"load",convertTrees);

// Utility function to add an event listener
function addEvent(o,e,f){
    if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
    else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
    else { return false; }
}

// utility function to set a global variable if it is not already set
function setDefault(name,val) {
    if (typeof(window[name])=="undefined" || window[name]==null) {
        window[name]=val;
    }
}

// Full expands a tree with a given ID
function expandTree(treeId) {
    var ul = document.getElementById(treeId);
    if (ul == null) { return false; }
    expandCollapseList(ul,nodeOpenClass);
}

// Fully collapses a tree with a given ID
function collapseTree(treeId) {
    var ul = document.getElementById(treeId);
    if (ul == null) { return false; }
    expandCollapseList(ul,nodeClosedClass);
}

// Expands enough nodes to expose an LI with a given ID
function expandToItem(treeId,itemId) {
    var ul = document.getElementById(treeId);
    if (ul == null) { return false; }
    var ret = expandCollapseList(ul,nodeOpenClass,itemId);
    if (ret) {
        var o = document.getElementById(itemId);
        if (o.scrollIntoView) {
            o.scrollIntoView(false);
        }
    }
}

// Performs 3 functions:
// a) Expand all nodes
// b) Collapse all nodes
// c) Expand all nodes to reach a certain ID
function expandCollapseList(ul,cName,itemId) {
    if (!ul.childNodes || ul.childNodes.length==0) { return false; }
    // Iterate LIs
    for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
        var item = ul.childNodes[itemi];
        if (itemId!=null && item.id==itemId) { return true; }
        if (item.nodeName == "LI") {
            // Iterate things in this LI
            var subLists = false;
            for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
                var sitem = item.childNodes[sitemi];
                if (sitem.nodeName=="UL") {
                    subLists = true;
                    var ret = expandCollapseList(sitem,cName,itemId);
                    if (itemId!=null && ret) {
                        item.className=cName;
                        return true;
                    }
                }
            }
            if (subLists && itemId==null) {
                item.className = cName;
            }
        }
    }
}

// Search the document for UL elements with the correct CLASS name, then process them
function convertTrees() {
    setDefault("treeClass","tree");
    setDefault("nodeClosedClass","liClosed");
    setDefault("nodeOpenClass","liOpen");
    setDefault("nodeBulletClass","liBullet");
    setDefault("nodeLinkClass","bullet");
    setDefault("preProcessTrees",true);
    if (preProcessTrees) {
        if (!document.createElement) { return; } // Without createElement, we can't do anything
        uls = document.getElementsByTagName("ul");
        for (var uli=0;uli<uls.length;uli++) {
            var ul=uls[uli];
            if (ul.nodeName=="UL" && ul.className==treeClass) {
                processList(ul);
            }
        }
    }
}

// Process a UL tag and all its children, to convert to a tree
function processList(ul) {
    if (!ul.childNodes || ul.childNodes.length==0) { return; }
    // Iterate LIs
    for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
        var item = ul.childNodes[itemi];
        if (item.nodeName == "LI") {
            // Iterate things in this LI
            var subLists = false;
            for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
                var sitem = item.childNodes[sitemi];
                if (sitem.nodeName=="UL") {
                    subLists = true;
                    processList(sitem);
                }
            }
            var s= document.createElement("SPAN");
            var t= '\u00A0'; // &nbsp;
            s.className = nodeLinkClass;
            if (subLists) {
                // This LI has UL's in it, so it's a +/- node
                if (item.className==null || item.className=="") {
                    item.className = nodeClosedClass;
                }
                // If it's just text, make the text work as the link also
                if (item.firstChild.nodeName=="#text") {
                    t = t+item.firstChild.nodeValue;
                    item.removeChild(item.firstChild);
                }
                s.onclick = function () {
                    this.parentNode.className = (this.parentNode.className==nodeOpenClass) ? nodeClosedClass : nodeOpenClass;
                    return false;
                }
            }
            else {
                // No sublists, so it's just a bullet node
                item.className = nodeBulletClass;
                s.onclick = function () { return false; }
            }
            s.appendChild(document.createTextNode(t));
            item.insertBefore(s,item.firstChild);
        }
    }
}

感谢。

网页的Ajax如下所示:

<script language="Javascript">

    function xmlhttpPost(strURL) {
        var xmlHttpReq = false;
        var self = this;

        // Mozilla / Safari.
        if (window.XMLHttpRequest) {
            self.xmlHttpReq = new XMLHttpRequest();
        }

        // IE.
        else if (window.ActiveXObject) {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        self.xmlHttpReq.open('POST', strURL, true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() {
            if (self.xmlHttpReq.readyState == 4) {
                updatePage(self.xmlHttpReq.responseText);                                                   
            }
        }
        self.xmlHttpReq.send(getQueryStr());
    }

    function getQueryStr() {
        queryStr = "action=toggleTree";
        return queryStr;
    }

    function updatePage(str) {            
        if (str == "false") {

           // Hide tree buttons and tree.
           document.getElementById("tree").style.visibility = "hidden";
           document.getElementById("expColTreeButtons").style.visibility = "hidden";
        }
        else {

           // Show tree buttons.
           document.getElementById("expColTreeButtons").style.visibility = "visible";

           // Show tree.
           document.getElementById("tree").style.visibility = "visible";
           document.getElementById("tree").innerHTML = str;

        }
    }

    function toggleTree() {

        // Make call to server to toggle tree.
        document.getElementById("tree").innerHTML = "<img src='/myDataSharer/images/myDataSharer_Wait.gif' alt='Growing tree' />"
        xmlhttpPost("/myDataSharer/toggleTree");

   }

上面的Ajax是从一个有三个按钮的表单触发的。 “显示/隐藏”按钮可以看到事物;另外两个按钮也包含在一个div中,但它们没问题。

<form>
                <input class = "treeButton" type="submit" value="Show / hide" onClick = "toggleTree(); return false;">
                <div id = "expColTreeButtons">
                    <input class = "treeButton" type="submit" value="Expand all" onClick = "expandTree('navTree'); return false;">
                    <br />
                    <input class = "treeButton" type="submit" value="Collapse all" onClick = "collapseTree('navTree'); return false;">
                    <br />
                </div>
            </form>

3 个答案:

答案 0 :(得分:0)

您的CSS看起来很好,就像可以在左侧生成示例的内容一样,因此它必须是显示和隐藏的HTML或JavaScript。 JavaScript是如何工作的?

答案 1 :(得分:0)

这不太可能是CSS问题,因为它是第一次工作。我敢打赌,问题在于服务器如何生成内容 - 即不为每个节点分配适当的属性。

答案 2 :(得分:0)

这个问题现在已经解决了。谢谢那些贡献的人。

解决方案是在div更新后重新处理Javascript树。