a2332579 发表于 2007-8-2 09:09

[求助]用Javascript脚本

*/ --------------------------------------------------------------------------------------<BR>*/ 出自: 编程中国  http://www.bc-cn.net<BR>*/ 作者: a2332579        QQ:240179387<BR>*/ 时间: 2007-8-2  编程论坛首发<BR>*/ 声明: 尊重作者劳动,转载请保留本段文字<BR>*/ --------------------------------------------------------------------------------------<BR><BR><BR>
<P>function Folder(folderDescription, hreference) {<BR>   //constant data;<BR>   this.desc = folderDescription;<BR>   this.hreference = hreference;<BR>   this.id = -1;<BR>   this.navObj = 0;<BR>   this.iconImg = 0;<BR>   this.nodeImg = 0;<BR>   this.isLastNode = 0;</P>
<P>   // dynamic data<BR>   this.isRoot = true;<BR>   this.isOpen = true;<BR>   this.iconSrc = "ftv2folderopen.gif";<BR>   this.iconRoot = "ftv2folderopen.gif"<BR>   this.children = new Array;<BR>   this.nChildren = 0;<BR>                                   <BR>   // methods<BR>   this.initialize = initializeFolder;<BR>   this.setState = setStateFolder;<BR>   this.addChild = addChild;<BR>   this.createIndex = createEntryIndex;<BR>   this.hide = hideFolder;<BR>   this.display = display;<BR>   this.renderOb = drawFolder;<BR>   this.totalHeight = totalHeight;<BR>   this.subEntries = folderSubEntries;<BR>   this.outputLink = outputFolderLink;<BR>}</P>
<P><BR>function setStateFolder(isOpen) { <BR>   var subEntries; <BR>   var totalHeight; <BR>   var fIt = 0; <BR>   var i = 0; <BR>   <BR>   if(isOpen == this.isOpen)<BR>       return;<BR>  <BR>   if(browserVersion == 2) {<BR>       totalHeight = 0 <BR>       for(i = 0; i &lt; this.nChildren; i++) <BR>           totalHeight = totalHeight + this.children[i].navObj.clip.height;<BR>       subEntries = this.subEntries();<BR> <BR>       if(this.isOpen)<BR>           totalHeight = 0 - totalHeight;</P>
<P>       for(fIt = this.id + subEntries + 1; fIt &lt; nEntries; fIt++)<BR>           indexOfEntries[fIt].navObj.moveBy(0, totalHeight);<BR>   }</P>
<P>   this.isOpen = isOpen;<BR>   propagateChangesInState(this);<BR>} </P>
<P><BR>function propagateChangesInState(folder) {   <BR>   var i = 0;<BR>   if(folder.isOpen) { <BR>       if(folder.nodeImg) {<BR>           if(folder.isLastNode)<BR>               folder.nodeImg.src = "images/ftv2mlastnode.gif";<BR>           else<BR>               folder.nodeImg.src = "images/ftv2mnode.gif";<BR>       }</P>
<P>       if(this.isRoot)<BR>           folder.iconImg.src = "images/ftv2mail.gif";<BR>       else <BR>           folder.iconImg.src = "images/ftv2folderopen.gif";<BR>          <BR>       for(i = 0; i &lt; folder.nChildren; i++) <BR>           folder.children[i].display();<BR>   } <BR>   else { <BR>       if(folder.nodeImg) {<BR>           if(folder.isLastNode)<BR>               folder.nodeImg.src = "images/ftv2plastnode.gif";<BR>           else<BR>               folder.nodeImg.src = "images/ftv2pnode.gif";<BR>       }</P>
<P>       if(this.isRoot)<BR>           folder.iconImg.src = "images/ftv2mail.gif";<BR>       else <BR>           folder.iconImg.src = "images/ftv2folderclosed.gif";<BR>   <BR>       for(i = 0; i &lt; folder.nChildren; i++) <BR>           folder.children[i].hide();<BR>   }<BR>} </P>
<P>function hideFolder() { <BR>    if(browserVersion == 1) { <BR>        if(this.navObj.style.display == "none")<BR>            return;</P>
<P>        this.navObj.style.display = "none";<BR>    }<BR>    else { <BR>        if(this.navObj.visibility == "hiden")<BR>            return;</P>
<P>        this.navObj.visibility = "hiden";<BR>    } </P>
<P>    this.setState(0);<BR>} </P>
<P><BR>function initializeFolder(level, lastNode, leftSide) { <BR>    var i = 0;<BR>    var j = 0;</P>
<P>    var numberOfFolders;<BR>    var numberOfDocs;</P>
<P>    nc = this.nChildren;<BR>    this.createIndex();</P>
<P>    var nc;<BR>    var auxEv = "";</P>
<P>    if(browserVersion &gt; 0)<BR>        auxEv = "&lt;A HREF='JavaScript: clickOnNode("+this.id+")'&gt;";//+<BR>    else<BR>        auxEv = "&lt;A&gt;";</P>
<P>    if(level &gt; 0) {<BR>        if(lastNode) { //the last 'brother' in the children array<BR>           this.renderOb(leftSide + auxEv + "&lt;IMG NAME='nodeIcon" + this.id + "' SRC='images/ftv2mlastnode.gif' WIDTH=16 HEIGHT=22 BORDER=0&gt;&lt;/A&gt;");<BR>           leftSide = leftSide + "&lt;IMG SRC='images/ftv2blank.gif' WIDTH=16 HEIGHT=22&gt;";<BR>           this.isLastNode = 1;<BR>        }<BR>        else {<BR>            this.renderOb(leftSide + auxEv + "&lt;IMG NAME='nodeIcon" + this.id + "' SRC='images/ftv2mnode.gif' WIDTH=16 HEIGHT=22 BORDER=0&gt;&lt;/A&gt;");<BR>            leftSide = leftSide + "&lt;IMG SRC='images/ftv2vertline.gif' WIDTH=16 HEIGHT=22&gt;";<BR>            this.isLastNode = 0;<BR>        }<BR>    }<BR>    else {<BR>        this.renderOb("");<BR>    }</P>
<P>    if(nc &gt; 0) {<BR>        level = level + 1;<BR>        for(i = 0; i &lt; this.nChildren; i++) {<BR>            if(i == this.nChildren-1)<BR>                this.children[i].initialize(level, 1, leftSide);<BR>            else<BR>                this.children[i].initialize(level, 0, leftSide);<BR>        }<BR>    }<BR>}<BR> </P>
<P>function drawFolder(leftSide) { <BR>    //alert(leftSide);<BR>    if(browserVersion == 2) {<BR>        if(!doc.yPos) <BR>          doc.yPos = 8;</P>
<P>        doc.write("&lt;LAYER ID='folder" + this.id + "' TOP=" + doc.yPos + " VISIBILITY=hiden&gt;");<BR>    } <BR>    doc.write("&lt;TABLE") </P>
<P>    if(browserVersion == 1) <BR>        doc.write(" ID='folder" + this.id + "' STYLE='position:block;' ");</P>
<P>    doc.write(" BORDER=0 CELLSPACING=0 CELLPADDING=0&gt;");<BR>    doc.write("&lt;TR&gt;&lt;TD&gt;");<BR>    doc.write(leftSide);<BR>    this.outputLink();<BR>    <BR>    if(leftSide != '') { <BR>        doc.write("&lt;IMG NAME='folderIcon" + this.id + "' ");<BR>       <BR>        if(leftSide == '') {<BR>            doc.write("SRC='images/" + this.iconRoot+"' BORDER=0&gt;&lt;/A&gt;");<BR>        }<BR>        else {<BR>            doc.write("SRC='images/" + this.iconSrc+"' BORDER=0&gt;&lt;/A&gt;");<BR>        }       <BR>    }<BR>    else<BR>    {        <BR>        doc.write("&lt;span id='folderIcon" + this.id + "' NAME='folderIcon" + this.id + "' &gt;");</P>
<P>    }<BR>    doc.write("&lt;/TD&gt;&lt;TD NOWRAP&gt;");<BR>  <BR>    doc.write("&lt;DIV CLASS=\"fldrroot\"&gt;");</P>
<P>    if(leftSide == '') { <BR>       if(USETEXTLINKS) {<BR>          //this.outputLink();<BR>          //doc.write(this.desc + "&lt;/A&gt;");<BR>       }<BR>       else <BR>         doc.write(this.desc);<BR>    }<BR>    else {<BR>        doc.write("&lt;A HREF='JavaScript: clickOnNode("+this.id+")'&gt;");<BR>        doc.write(this.desc + "&lt;/A&gt;");<BR>    }</P>
<P>    doc.write("&lt;/DIV&gt;");<BR>    doc.write("&lt;/TD&gt;");<BR>    doc.write("&lt;/TABLE&gt;");</P>
<P>    if(browserVersion == 2) { <BR>        doc.write("&lt;/LAYER&gt;");<BR>    } </P>
<P>    if(browserVersion == 1) { <BR>        this.navObj = doc.all["folder"+this.id];<BR>        this.iconImg = doc.all["folderIcon"+this.id] <BR>        this.nodeImg = doc.all["nodeIcon"+this.id] <BR>    }<BR>    else if(browserVersion == 2) { <BR>        this.navObj = doc.layers["folder"+this.id]; <BR>        this.iconImg = this.navObj.document.images["folderIcon"+this.id];<BR>        this.nodeImg = this.navObj.document.images["nodeIcon"+this.id]; <BR>        doc.yPos = doc.yPos + this.navObj.clip.height;<BR>    }<BR>} <BR> </P>
<P>function outputFolderLink() {<BR>    if(this.hreference) { <BR>        doc.write("&lt;A HREF='" + this.hreference + "' TARGET=\"main\" ") </P>
<P>        if(browserVersion &gt; 0) {<BR>            doc.write("onClick='JavaScript: clickOnFolder("+this.id+")'")//folder <BR>        }<BR>        doc.write("&gt;") <BR>    } <BR>    else <BR>        doc.write("&lt;A&gt;")<BR>} </P>
<P> <BR>function addChild(childNode) {<BR>    this.children[this.nChildren] = childNode;<BR>    this.nChildren++;</P>
<P>    return(childNode);<BR>} </P>
<P><BR>function folderSubEntries() { <BR>    var i = 0;<BR>    var se = this.nChildren;<BR>    <BR>    for(i = 0; i &lt; this.nChildren; i++) {<BR>        if(this.children[i].children) //is a folder <BR>            se = se + this.children[i].subEntries();<BR>    }<BR>    <BR>    return(se)<BR>} <BR> <BR> <BR>// Definition of class Item (a document or link inside a Folder) </P>
<P>function Item(itemDescription, hreference, itemLink, itemImg) // Constructor <BR>{ <BR>    // constant data <BR>    this.desc = itemDescription <BR>    this.link = itemLink <BR>    this.hreference = hreference;<BR>    this.id = -1;     //initialized in initalize() <BR>    this.navObj = 0;  //initialized in render() <BR>    this.iconImg = 0; //initialized in render() </P>
<P>    this.iconSrc = itemImg;</P>
<P>    // methods<BR>    this.initialize = initializeItem ;<BR>    this.createIndex = createEntryIndex;<BR>    this.hide = hideItem;<BR>    this.display = display;<BR>    this.renderOb = drawItem;<BR>    this.totalHeight = totalHeight;<BR>} </P>
<P><BR>function hideItem() {<BR>    if(browserVersion == 1) {<BR>        if(this.navObj.style.display == "none") <BR>            return;<BR>        this.navObj.style.display = "none" <BR>    }<BR>    else {<BR>        if(this.navObj.visibility == "hiden")<BR>            return;<BR>        this.navObj.visibility = "hiden";<BR>    }<BR>} <BR> </P>
<P>function initializeItem(level, lastNode, leftSide) {<BR>    this.createIndex();</P>
<P>    if(level &gt; 0) {<BR>        if(lastNode) { //the last 'brother' in the children array <BR>            this.renderOb(leftSide + "&lt;IMG SRC='images/ftv2lastnode.gif' WIDTH=16 HEIGHT=22&gt;") <BR>            leftSide = leftSide + "&lt;IMG SRC='images/ftv2blank.gif' WIDTH=16 HEIGHT=22&gt;"  <BR>        }<BR>        else {<BR>            this.renderOb(leftSide + "&lt;IMG SRC='images/ftv2node.gif' WIDTH=16 HEIGHT=22&gt;") <BR>            leftSide = leftSide + "&lt;IMG SRC='images/ftv2vertline.gif' WIDTH=16 HEIGHT=22&gt;" <BR>        }<BR>    }<BR>    else <BR>        this.renderOb("")   <BR>}</P>
<P><BR>function drawItem(leftSide) { <BR>    if(browserVersion == 2)<BR>        doc.write("&lt;LAYER ID='item" + this.id + "' TOP=" + doc.yPos + " VISIBILITY=hiden&gt;");//link<BR>     <BR>    doc.write("&lt;TABLE ");<BR>    if(browserVersion == 1) <BR>        doc.write(" ID='item" + this.id + "' STYLE='position:block;' ");<BR>    doc.write(" BORDER=0 CELLSPACING=0 CELLPADDING=0&gt;");<BR>    doc.write("&lt;TR&gt;&lt;TD&gt;");<BR>    doc.write(leftSide);</P>
<P>    if(this.link != "")<BR>        doc.write("&lt;A HREF=" + this.link + "&gt;");<BR>  <BR>    doc.write("&lt;IMG ID='itemIcon"+this.id+"' ") <BR>    doc.write("SRC='images/"+this.iconSrc+"' BORDER=0&gt;") </P>
<P>    if(this.link != "")<BR>        doc.write("&lt;/A&gt;");<BR>  <BR>    doc.write("&lt;/TD&gt;&lt;TD NOWRAP&gt;");      <BR>    doc.write("&lt;DIV CLASS=\"fldritem\"&gt;");<BR>    if(USETEXTLINKS) {<BR>        if(this.link != "")<BR>            doc.write("&lt;A HREF=" + this.link + "&gt;" + this.desc + "&lt;/A&gt;");<BR>        else<BR>            doc.write(this.desc);<BR>    }<BR>    else {<BR>        doc.write(this.desc);<BR>    }</P>
<P>    doc.write("&lt;/DIV&gt;");<BR>    doc.write("&lt;/TABLE&gt;") <BR>     <BR>    if(browserVersion == 2)<BR>        doc.write("&lt;/layer&gt;");</P>
<P>    if(browserVersion == 1) {<BR>        this.navObj = doc.all["item"+this.id] <BR>        this.iconImg = doc.all["itemIcon"+this.id] <BR>    }<BR>    else if(browserVersion == 2) {<BR>        this.navObj = doc.layers["item"+this.id];<BR>        this.iconImg = this.navObj.document.images["itemIcon"+this.id];<BR>        doc.yPos = doc.yPos+this.navObj.clip.height;<BR>    }<BR>} <BR> <BR> <BR>// Methods common to both objects (pseudo-inheritance)     <BR>function display() { <BR>    if(browserVersion == 1) <BR>        this.navObj.style.display = "block";<BR>    else<BR>        this.navObj.visibility = "show";<BR>}</P>
<P><BR>function createEntryIndex() { <BR>    this.id = nEntries;<BR>    indexOfEntries[nEntries] = this;<BR>    nEntries++;<BR>}</P>
<P><BR>// total height of subEntries open <BR>function totalHeight() { //used with browserVersion == 2 <BR>    var h = this.navObj.clip.height;<BR>    var i = 0;</P>
<P>    if(this.isOpen) { // is a folder and _is_ open <BR>        for(i = 0 ; i &lt; this.nChildren; i++)  <BR>            h = h + this.children[i].totalHeight();<BR>    }</P>
<P>    return h <BR>} <BR> <BR> <BR>// Events <BR> <BR>function clickOnFolder(folderId) { <BR>    var clicked = indexOfEntries[folderId];</P>
<P>    if(!clicked.isOpen)<BR>        clickOnNode(folderId);</P>
<P>    if(clicked.isSelected) <BR>        return;<BR>} <BR> </P>
<P>function clickOnNode(folderId) { <BR>    var clickedFolder = 0;<BR>    var state = 0;</P>
<P>    clickedFolder = indexOfEntries[folderId];<BR>    state = clickedFolder.isOpen;<BR> <BR>    clickedFolder.setState(!state); //open&lt;-&gt;close<BR>}</P>
<P><BR>function initializeDocument() { <BR>    if(doc.all)<BR>        browserVersion = 1; //IE4<BR>    else if(doc.layers)<BR>        browserVersion = 2; //NS4<BR>    else <BR>        browserVersion = 0; //other</P>
<P>    ax0.initialize(0, 1, "");<BR>    ax0.display();</P>
<P>    if(browserVersion &gt; 0) {<BR>        doc.write("&lt;LAYER TOP="+indexOfEntries[nEntries-1].navObj.top+"&gt;&amp;nbsp;&lt;/LAYER&gt;");</P>
<P>        // close the whole tree<BR>        clickOnNode(0);<BR>    <BR>        // open the root folder <BR>        clickOnNode(0);<BR>    } <BR>} <BR> </P>
<P>// Auxiliary Functions for Folder-Treee backward compatibility   <BR>function gFld(description, hreference) { <BR>    folder = new Folder(description, hreference);<BR>    return(folder);<BR>}</P>
<P><BR>function gLnk(target, description, linkData, itemImg) { <BR>    fullLink = "" <BR>   <BR>    if(target == 0) {<BR>        if(linkData != "")<BR>            fullLink = "'"+linkData+"' TARGET=\"main\"";<BR>        else<BR>            fullLink = "";<BR>    }<BR>    else if(target == 1) {<BR>        if(linkData != "")<BR>            fullLink = "'"+linkData+"' TARGET=\"_top\"";<BR>        else<BR>            fullLink = "";<BR>    }</P>
<P>    linkItem = new Item(description, linkData, fullLink, itemImg)   <BR>    return linkItem <BR>} </P>
<P><BR>function insFld(parentFolder, childFolder) { <BR>    return(parentFolder.addChild(childFolder));<BR>}</P>
<P><BR>function insDoc(parentFolder, document) { <BR>    parentFolder.addChild(document);<BR>}</P>
<P><BR>// Global variables <BR> <BR>USETEXTLINKS = 1;<BR>indexOfEntries = new Array;<BR>nEntries = 0;<BR>doc = document;<BR>browserVersion = 0;<BR>selectedFolder = 0;</P>

lmhllr 发表于 2007-8-2 09:16

这个是干什么的??没有注释和说明,太长,看不了

a2332579 发表于 2007-8-2 09:33

是一个树型导航!<BR>我也是用别人的,但是我导航栏超过500条后,就提示,该页面脚本导致IE浏览器速度减慢,在网上查了一下,说是循环次数太多导致,并不是死循环

页: [1]

编程论坛