Jquery click Li to prevent repeated loading

Source: Internet
Author: User
After clicking, Li calls ajax to load the content and expands the content. Li is not like the button, which can be set to disabled after responding to the click event.
Because loading content is slow, users may accidentally click twice on Li, so they will request twice. This is what we don't want to see.
Today, I sent two demos to the JavaScript-jquery group by copying the clicked Li node.
Obviously, the function is not suitable for me.
I am not doing anything. I thought about it. Why not add the loading image (before Ajax loading) When I click Li to determine whether the IMG element exists on the Li node,
If not, load the Ajax content; otherwise, do not process it.
Passed the test, \ (^ o ^ )/.

View sourceprint?

001 <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

002 <HTML xmlns = "http://www.w3.org/1999/xhtml">

003 <Head>

004 <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

005 <Title> jquery Ajax binding event loading </title>

006 <Style>

007 * {Margin: 0px; padding: 0px ;}

008 Body {font-size: 12px; font-family: verdana, Arial, Helvetica, sans-serif ;}

009 # Container {position: relative; overflow: hidden ;}

010 # Header {Height: 100px; line-Height: 100px; Background: # dedede; padding-left: 20px; position: absolute; left: 0px; top: 0px; position: fixed! Important; width: 100%; margin-bottom: 20px ;}

011 # Header H1 {font-size: 120%; color: # FFF ;}

012 # Header H1 span {font-size: 75%}

013 # Siderbar {width: 240px; margin-left: 10px; Border: 1px solid # DDD; position: absolute; left: 0px; top: 120px; position: fixed! Important; Z-index: 9999 ;}

014 # Siderbar_box {padding: 10px; overflow: auto}

015 # Siderbar H4 {Background: # Eee; color: #666; padding: 5px 10px ;}

016 # Siderbar_box ul {list-style: none; margin-left: 10px ;}

017 # Content {padding: 120px 0px 0px 260px; overflow: auto; _ padding: 120px 0px 0px 280px ;}

018 # Content ul {list-style: none ;}

019 # Content ul Li {border: 1px solid # DDD; cursor: pointer; display: block}

020 # Content ul Li span {display: block; padding: 5px ;}

021 # Content ul Li table {margin: 5px auto; padding: 0px; border-collapse: collapse; Border: 1px solid #999; width: 98% ;}

022 # Content ul Li table TD {/* padding: 2px 5px; */Border: 1px solid #999 ;}

023 # Content_footer {text-align: center ;}

024 . Focus {padding: 2px 5px; boder: 1px solid # AAA; Background: # fafafa ;}

025 . Highlight {color: # FFF; Background: # 0099ff}

026 </Style>

027 <SCRIPT src = "jquery-1.3.2.min.js"> </SCRIPT>

028 <SCRIPT>

029 $ (Function (){

030 Setheight ("# siderbar", 130); // you can specify the height of the sidebar.

031 $ (Window). Resize (function () {setheight ("# siderbar", 130)}); // reset the sidebar height when the window changes

032 $. Get ("sider.html", function (data ){

033 $ ('# Siderbar_box'). append (data );

034 });

035 /* Set the ID height */

036 Function setheight (ID, h ){

037 VaR success wheight = $ (window). Height ();

038 ((Id).css ({"height" windowheight-h) + "PX "});

039 }

040 // View the loaded Li after binding

041 $ ("# Siderbar_box ul Li a"). Live ("click", function (){

042 VaR $ current = $ (this );

043 VaR $ currentli = $ (this). Parent ();

044 If ($ currentli. Children ("Ul"). ATTR ("ID") = undefined) // Ajax loading is required for the first time

045 {

046 $ Currentli. siblings (). Children ("Ul"). slideup ('fast ');

047 $ Currentli. siblings (). Children ("A"). removeclass ("Focus ");

048 $. Get ("chapter.html", function (data ){

049 $ Current. addclass ("Focus"). Parent (). append (data );

050 Showchapter (); // display the main content in the content

051 });

052

053 } Else {

054 $ Currentli. siblings (). Children ("Ul"). slideup ('fast ');

055 $ Currentli. siblings (). Children ("A"). removeclass ("Focus ");

056 If ($ currentli. Children ("Ul"). Is (": visible") // in the expanded State

057 {

058

059 $ Current. removeclass ("Focus"). Parent (). Children ("Ul"). slideup ('save ');

060 } Else {

061

062 $ Current. addclass ("Focus"). Parent (). Children ("Ul"). slidedown ('low ');

063 Showchapter ();

064 }

065 }

066 });

067 // Content displays the list title

068 Function showchapter (){

069 $. Get ("chapter.html", function (data ){

070 $ ("# Content" pai.html (data );

071 $ ("# Content ul li"). Live ("click", function () {// bind the loaded title to click the event

072 $ Current = $ (this );

073 If ($ current. Children ("table"). ATTR ("ID") = undefined) // click the title for the first Ajax Load

074 {

075 If ($ current. Children ("span"). Find ("IMG"). Size () <1) // load content only once to prevent multiple requests from loading

076 {

077 $ Current. Children ("span"). append ("

078 $. Get ("table.html", function (data ){

079 $ Current. append (data). Children ("span"). addclass ("highlight"). Find ("IMG"). Remove (); // after loading, remove the loaded image.

080 });

081 }

082 } Else {

083

084 If ($ current. Children ("table"). Is (": visible "))

085 {

086 $ Current. Children ("span"). removeclass ("highlight"). Next ("table"). Hide ();

087

088 } Else {

089

090 $ Current. Children ("span"). addclass ("highlight"). Next ("table"). Show ();

091 }

092 }

093

094 });

095 });

096 }

097 });

098 </SCRIPT>

099 </Head>

100 <Body>

101 <Div id = "Container">

102 <Div id = "Header">

103 <Div id = "siderbar">

104 <H4> course </H4>

105 <Div id = "siderbar_box">

106

107 </Div>

108 </Div>

109 <Div id = "content">

110 <Div id = "content_footer"> </div>

111 </Div>

112

113 </Div>

114 </Body>

115 </Html>

This article is transferred from www.35java.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.