搜尋
首頁web前端html教學Jquery树形菜单默认全部展开了,需要缩起,求解_html/css_WEB-ITnose

个人网上下了一段Jquery树形菜单代码,默认打开页面是全部展开的,需要全部缩起来,只显示第一级,求解修改哪里?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title><!--图标样式--><link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" /><!--主要样式--><link rel="stylesheet" type="text/css" href="css/style.css" /><script type="text/javascript" src="js/jquery-1.7.2.min.js"></script><script type="text/javascript">$(function(){    $('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');    $('.tree li.parent_li > span').on('click', function (e) {	        var children = $(this).parent('li.parent_li').find(' > ul > li');        if (children.is(":visible")) {            children.hide('fast');            $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');        } else {            children.show('fast');            $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');        }        e.stopPropagation();		    });});</script></head><body><div class="tree well"><ul>	<li>		<span><i class="icon-minus-sign"></i> Parent2</span> <a style="display:none" href="#">Test</a>		<ul>			<li>				<span ><i class="icon-globe"></i> Child</span> <a style="display:none" href="#">Test</a>			</li>			<li>				<span><i class="icon-globe"></i> Child</span> <a style="display:none" href="#">Test</a>			</li>		</ul>	</li>	<li>		<span><i class="icon-minus-sign"></i> Parent2</span> <a style="display:none" href="#">Test</a>		<ul>			<li>				<span><i class="icon-globe"></i> Child</span> <a style="display:none" href="#">Test</a>			</li>		</ul>	</li>	<li>		<span><i class="icon-globe"></i> Parent2</span> 			</li></ul></div></body></html>


回复讨论(解决方案)

 没有人帮我嘛。。

.addClass('icon-plus-sign').removeClass('icon-minus-sign');

这是  增加样式与去除样式。jq里用show()与hide()来显示与隐藏

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title> <!--图标样式--><link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" /> <!--主要样式--><link rel="stylesheet" type="text/css" href="css/style.css" /> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script><script type="text/javascript">$(function(){    $('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');    $('.tree li.parent_li > span').live('click', function (e) {             var children = $(this).parent('li.parent_li').find(' > ul > li');        if (children.is(":visible")) {            children.hide('fast');            $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');        } else {            children.show('fast');            $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');        }        e.stopPropagation();             });}); </script> </head><body> <div class="tree well"><ul>    <li>        <span><i class="icon-minus-sign"></i> Parent2</span> <a style="display:none" href="#">Test</a>        <ul>            <li>                <span ><i class="icon-globe"></i> Child</span> <a style="display:none" href="#">Test</a>            </li>            <li>                <span><i class="icon-globe"></i> Child</span> <a style="display:none" href="#">Test</a>            </li>        </ul>    </li>    <li>        <span><i class="icon-minus-sign"></i> Parent2</span> <a style="display:none" href="#">Test</a>        <ul>            <li>                <span><i class="icon-globe"></i> Child</span> <a style="display:none" href="#">Test</a>            </li>        </ul>    </li>    <li>        <span><i class="icon-globe"></i> Parent2</span>              </li></ul></div> <script>$('ul li ul:not(:first) li').hide()</script></body></html>

.addClass('icon-plus-sign').removeClass('icon-minus-sign');

这是  增加样式与去除样式。jq里用show()与hide()来显示与隐藏



我给你CSS 你看看  那都是替换展开收缩的加减图标用的,icon的css都是图标。
a:link { color: #000000; text-decoration: none;}a:visited { color: #000000; text-decoration: none;}a:hover { color:#999999; text-decoration: underline;}a:active { color: #000000; text-decoration: none;}.tree {    min-height:20px;    padding:19px;    margin-bottom:20px;    background-color:#fbfbfb;    border:1px solid #999;    -webkit-border-radius:4px;    -moz-border-radius:4px;    border-radius:4px;    -webkit-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05);    -moz-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05);    box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05)}.tree li {    list-style-type:none;    margin:0;    padding:10px 5px 0 5px;    position:relative}.tree li::before, .tree li::after {    content:'';    left:-20px;    position:absolute;    right:auto}.tree li::before {    border-left:1px solid #999;    bottom:50px;    height:100%;    top:0;    width:1px}.tree li::after {    border-top:1px solid #999;    height:20px;    top:25px;    width:25px}.tree li span {    -moz-border-radius:5px;    -webkit-border-radius:5px;    border:1px solid  #999999;    border-radius:5px;    display:inline-block;    padding:3px 8px;    text-decoration:none}.tree li.parent_li>span {    cursor:pointer}.tree>ul>li::before, .tree>ul>li::after {    border:0}.tree li:last-child::before {    height:30px}.tree li.parent_li>span:hover, .tree li.parent_li>span:hover+ul li span {    background:#eee;    border:1px solid #94a0b4;    color:#000}

$(function(){    $('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');    $('.tree li.parent_li > span').live('click', function (e) {             var children = $(this).parent('li.parent_li').find(' > ul > li');        if (children.is(":visible")) {            children.hide('fast');            $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');        } else {            children.show('fast');            $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');        }        e.stopPropagation();             });});  <script>$('ul li ul:not(:first) li').hide()</script>



Hi  非常感谢! 我要去学习一下这里的live 和 on 作用  ,<script>$('ul li ul li').hide()</script> 全部缩进。
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
HTML屬性的目的是什麼?HTML屬性的目的是什麼?May 07, 2025 am 12:01 AM

htmlattributeseresene forenhancingwebelements'functionalityandAppearance.TheyAdDinformationTodeFineBehavior,外觀和互動,使網站互動,響應式,visalalyAppealing.AttributesLikutesLikeSlikEslikesrc,href,href,href,類,類型,類型,和dissabledtransfransformformformformformformformformformformformformformformforment

您如何在HTML中創建列表?您如何在HTML中創建列表?May 06, 2025 am 12:01 AM

toCreateAlistInHtml,useforforunordedlistsandfororderedlists:1)forunorderedlists,wrapitemsinanduseforeachItem,RenderingeringAsabulletedList.2)fororderedlists,useandfornumberedlists,useandfornumberedlists,casundfornumberedlists,casundfornthetthetthetthetthetthetthetttributefordforderfordforderforderentnumberingsnumberingsnumberingStys。

HTML行動:網站結構的示例HTML行動:網站結構的示例May 05, 2025 am 12:03 AM

HTML用於構建結構清晰的網站。 1)使用標籤如、、定義網站結構。 2)示例展示了博客和電商網站的結構。 3)避免常見錯誤如標籤嵌套不正確。 4)優化性能通過減少HTTP請求和使用語義化標籤。

您如何將圖像插入HTML頁面?您如何將圖像插入HTML頁面?May 04, 2025 am 12:02 AM

toinsertanimageIntoanhtmlpage,usethetagwithsrcandaltattributes.1)usealttextforAcccessibilityandseo.2)instementRcsetForresponSiveImages.3)applylazyloadingWithLoadingWithLoading =“ lazy” tooptimizeperformance.4)tooptimizeperformance.4)

HTML的目的:啟用Web瀏覽器可以顯示內容HTML的目的:啟用Web瀏覽器可以顯示內容May 03, 2025 am 12:03 AM

HTML的核心目的在於讓瀏覽器理解並展示網頁內容。 1.HTML通過標籤定義網頁結構和內容,如、到、等。 2.HTML5增強了多媒體支持,引入了和標籤。 3.HTML提供了表單元素,支持用戶交互。 4.優化HTML代碼可提升網頁性能,如減少HTTP請求和壓縮HTML。

為什麼HTML標籤對Web開發很重要?為什麼HTML標籤對Web開發很重要?May 02, 2025 am 12:03 AM

htmltagsareessentialforwebdevelopmentastheyandendenhancewebpages.1)semantictagsimproveaccessibilityandseo.2)semanteLayOut,語義和互動性。 3)poseriblesibilityandseoandseo.3)poseriblesoftagscanoftagscanoftagscanoptagscanoptimizeperefeneandimizeanDenSuroceRecRoscRoss-BrowserCrowserCercerComercompatibility。

說明將一致的編碼樣式用於HTML標籤和屬性的重要性。說明將一致的編碼樣式用於HTML標籤和屬性的重要性。May 01, 2025 am 12:01 AM

一致的HTML編碼風格很重要,因為它提高了代碼的可讀性、可維護性和效率。 1)使用小寫標籤和屬性,2)保持一致的縮進,3)選擇並堅持使用單引號或雙引號,4)避免在項目中混合使用不同風格,5)利用自動化工具如Prettier或ESLint來確保風格的一致性。

如何在 Bootstrap 4 中實現多項目輪播?如何在 Bootstrap 4 中實現多項目輪播?Apr 30, 2025 pm 03:24 PM

在Bootstrap4中實現多項目輪播的解決方案在Bootstrap4中實現多項目輪播並不是一件簡單的事情。雖然Bootstrap...

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器