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

标签 css image tree icons

不知道有没有人能帮忙。我的一个页面中的 HTML div 包含一个树形控件,它根据用户按下的按钮显示或隐藏。该按钮触发一个 Ajax 事件,该事件在服务器上设置一个变量以显示或隐藏树,以便持久保存状态。

但这就是问题所在;重新显示树时,展开/折叠支架的图标不存在。到目前为止,我还无法弄清楚为什么会这样。

树如下图所示:第一个图形显示了树的本来面目,第二个显示了隐藏并重新显示后的树。

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 

树本身位于名为“tree”的 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>

最佳答案

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

关于css - 在 Ajax 更新的 div 中显示 CSS 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/634171/

相关文章:

algorithm - 正确性证明 : Algorithm for diameter of a tree in graph theory

css - 将 css 变量与 scss 变暗和变亮过滤器一起使用

image - 如何在 GridView 中显示多列图像?

javascript - Canvas 到图像 src 上的 base64

html - 为什么背景图片没有填满整个div

SVN:仅在目录上设置属性

algorithm - 在二叉树中找到最大的不相交叶到叶路径之和

html - 将标题标记顺序与视觉和概念权重分开?

html - CSS 选择器 : styling link which contains img

css - 跨浏览器 CSS 边距和 float 定位