मैं जावास्क्रिप्ट का उपयोग करके <div> का विस्तार और पतन कैसे कर सकता हूं?


96

मैंने अपनी साइट पर एक सूची बनाई है। यह सूची एक फ़ॉरच लूप द्वारा बनाई गई है जो मेरे डेटाबेस से जानकारी के साथ बनाता है। प्रत्येक आइटम विभिन्न वर्गों के साथ एक कंटेनर है, इसलिए यह 1, 2, 3 जैसी कोई सूची नहीं है ... आदि मैं जानकारी के साथ दोहराए जाने वाले अनुभागों को सूचीबद्ध कर रहा हूं। प्रत्येक अनुभाग में, एक उपधारा है। सामान्य बिल्ड निम्नानुसार है:

<div>
    <fieldset class="majorpoints" onclick="majorpointsexpand($(this).find('legend').innerHTML)">
    <legend class="majorpointslegend">Expand</legend>
    <div style="display:none" >
        <ul>
            <li></li>
            <li></li>
        </ul>
    </div>
</div>

इसलिए, मैं onclick = "majorpointsexpand ($ (यह) .find ('किंवदंती')। InnerHTML) के साथ एक फ़ंक्शन को कॉल करने का प्रयास कर रहा हूं।"

डिव मैं जो हेरफेर करने की कोशिश कर रहा हूं वह शैली = "प्रदर्शन: कोई नहीं" डिफ़ॉल्ट रूप से है, और मैं इसे क्लिक पर दिखाई देने के लिए जावास्क्रिप्ट का उपयोग करना चाहता हूं।

"$ (यह) .find ('किंवदंती')। इनरएचटीएमएल" इस मामले में, "विस्तार" फ़ंक्शन में एक तर्क के रूप में पारित करने का प्रयास कर रहा है।

यहाँ जावास्क्रिप्ट है:

function majorpointsexpand(expand)
    {
        if (expand == "Expand")
            {
                document.write.$(this).find('div').style = "display:inherit";
                document.write.$(this).find('legend').innerHTML = "Collapse";
            }
        else
            {
                document.write.$(this).find('div').style = "display:none";
                document.write.$(this).find('legend').innerHTML = "Expand";
            }
    }

मुझे लगभग 100% यकीन है कि मेरी समस्या सिंटैक्स है, और मेरे पास जावास्क्रिप्ट के काम करने के तरीके के बारे में बहुत कुछ नहीं है।

मेरे पास jQuery दस्तावेज़ के साथ जुड़ा हुआ है:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

में <head></head>खंड।


2
मुझे लगता है कि आप जो हासिल करने की कोशिश कर रहे हैं वह एक समझौते है jqueryui.com/accordion
Marc

1
$ यह मुझे "एचटीएमएल तत्व" के संबंध में कहने की कोशिश कर रहा है जो फ़ंक्शन के अंदर चालू है।
रयान मोर्टेंसन

1
@hungerpain - मुझे लगता है कि पूछने वाला jQuery के लिए नया हो सकता है और केवल कोष्ठक को भूल सकता है $(this)। उम्मीद है की यह मदद करेगा।
jmort253

2
मुझे लगता है कि आपको पहले jQuery के बारे में अधिक अध्ययन करना चाहिए। जाहिरा तौर पर आप jQuery और जावास्क्रिप्ट के बीच अंतर के बारे में ज्यादा नहीं जानते हैं
tom10271

1
@aokaddaoc आप बिलकुल सही थे;)
रयान मोर्टेंसन

जवाबों:


181

ठीक है, इसलिए आपको यहां दो विकल्प मिले हैं:

  1. JQuery यूआई के समझौते का उपयोग करें - इसका अच्छा, आसान और तेज़। अधिक जानकारी यहाँ देखें
  2. या, यदि आप अभी भी अपने आप से ऐसा करना चाहते हैं, तो आप fieldsetइसे (वैसे भी इसके लिए इसका उपयोग करने के लिए शब्दार्थ अधिकार नहीं) निकाल सकते हैं और अपने आप से एक संरचना बना सकते हैं।

यहां बताया गया है कि आप ऐसा कैसे करते हैं। इस तरह एक HTML संरचना बनाएँ:

<div class="container">
    <div class="header"><span>Expand</span>

    </div>
    <div class="content">
        <ul>
            <li>This is just some random content.</li>
            <li>This is just some random content.</li>
            <li>This is just some random content.</li>
            <li>This is just some random content.</li>
        </ul>
    </div>
</div>

इस सीएसएस के साथ: (यह .contentपेज लोड होने पर सामान को छिपाने के लिए है ।

.container .content {
    display: none;
    padding : 5px;
}

फिर, jQuery का उपयोग करके, clickहेडर के लिए एक घटना लिखें ।

$(".header").click(function () {

    $header = $(this);
    //getting the next element
    $content = $header.next();
    //open up the content needed - toggle the slide- if visible, slide up, if not slidedown.
    $content.slideToggle(500, function () {
        //execute this after slideToggle is done
        //change text of header based on visibility of content div
        $header.text(function () {
            //change text based on condition
            return $content.is(":visible") ? "Collapse" : "Expand";
        });
    });

});

यहाँ एक डेमो है: http://jsfiddle.net/hungerpain/eK8X5/7/


9
यदि पृष्ठ पर एक से अधिक DIV तत्व है तो +1 इस समस्या को हल करेगा। दूसरे शब्दों में, चूंकि आप क्लिक किए गए हेडर के भीतर सामग्री को लक्षित कर रहे हैं, इसलिए यह अच्छी तरह से मापता है।
jmort253

2
फ़ील्डसेट अत्यावश्यक नहीं है। मैं इससे छुटकारा पाऊंगा और बस एक सीमा का उपयोग करूंगा। यह उत्कृष्ट है क्योंकि यह मेरे द्वारा क्लिक किए गए हेडर के सापेक्ष डिव का चयन करता है, जो इस तथ्य के कारण महत्वपूर्ण है कि मेरे पास उपयोगकर्ता सेटिंग्स और अन्य कारकों के आधार पर सूचीबद्ध वस्तुओं के कई अलग-अलग नंबर हो सकते हैं।
रयान मोर्टेंसन

1
@Unipartisandev इसको देखें: jsfiddle.net/hungerpain/476Nq पूरा भाग उदाहरण:
krishwader

मैं वास्तव में सहायता के लिए आभारी हूं। साइट के अन्य भाग होंगे जिन्हें किसी भी संदेह का उपयोग करने की आवश्यकता नहीं होगी, भले ही यह एक विशेष चीज सभी या किसी अन्य डिस्प्ले उदाहरण से अधिक हो। मुझे अभी भी समस्या हो रही है। मेरा jQuery पुराना था और लोड नहीं हो रहा था। यह तय है, लेकिन मैं अभी भी यह काम नहीं कर रहा हूँ। मैंने अब एक अच्छे घंटे के साथ इसे गड़बड़ कर दिया है, मैं इस पर सोऊंगा। शायद कल यह मुझसे टकराएगा।
रयान मोर्टेंसन

अद्भुत, धन्यवाद। बहुत समय बचा लिया!
तुलसी मूसा

21

कैसा रहेगा:

jQuery:

$('.majorpoints').click(function(){
    $(this).find('.hider').toggle();
});

एचटीएमएल

<div>
  <fieldset class="majorpoints">
    <legend class="majorpointslegend">Expand</legend>
    <div class="hider" style="display:none" >
        <ul>
            <li>cccc</li>
            <li></li>
        </ul>
    </div>
</div>

बेला

इस तरह से आप क्लिक ईवेंट को उस .majorpointsकक्षा में बाँध रहे हैं जिसे आपको हर बार HTML में लिखना नहीं पड़ता है।


हाय raam86, मैं इसे एक कदम आगे ले जाऊँगा और एक आईडी के बजाय एक वर्ग का उपयोग करते हुए div पर .find यदि पूछने वाले के पेज पर इन फ़ील्ड्स में से कई हैं, तो वह उस पर क्लिक किए गए विशिष्ट फ़ील्ड से संबंधित किसी एक के लिए राइडर को लक्षित करना चाहेगा। उम्मीद है की यह मदद करेगा! :) उदाहरण के लिए, आप मूल डिव का संदर्भ प्राप्त करने के लिए .closest का उपयोग कर सकते हैं, फिर। क्लास के बजाय div का पता लगाने के लिए .find का उपयोग करें।
jmort253

1
मुझे पता है कि यह 3 बजे है;), लेकिन मैंने अभी देखा कि आप अभी भी अपने jsFiddle में आईडी का उपयोग कर रहे हैं। यह अनिर्धारित व्यवहार के परिणामस्वरूप W3C कल्पना कहती है कि प्रत्येक आईडी अद्वितीय होनी चाहिए। यदि आप हाइडर को एक वर्ग में बदलते हैं, तो यह कीड़े या विषम, अन्य ब्राउज़रों में अस्पष्टीकृत व्यवहार के लिए अधिक प्रतिरक्षा होगा। उम्मीद है की यह मदद करेगा!
jmort253

यह वास्तव में $ होना चाहिए ('। majorpointlegend')। क्लिक करें (फंक्शन () {$ (यह) .parent ()। Find ('hider')। टॉगल ();}); या फिर जब फ़ील्डसेट में किसी भी स्थान पर क्लिक किया जाता है, तो वह ढह जाएगा।
१at:०at पर अवतता

7

तो, सबसे पहले, अपने जावास्क्रिप्ट भी jQuery का उपयोग नहीं कर रहा है। ऐसा करने के लिए कुछ तरीके हैं। उदाहरण के लिए:

पहला तरीका, jQuery का उपयोग करते हुए toggle विधि :

<div class="expandContent">
        <a href="#">Click Here to Display More Content</a>
 </div>
<div class="showMe" style="display:none">
        This content was hidden, but now shows up
</div>

<script>  
    $('.expandContent').click(function(){
        $('.showMe').toggle();
    });
</script>

jsFiddle: http://jsfiddle.net/pM3DF/

दूसरा तरीका बस jQuery का उपयोग करना है show विधि :

<div class="expandContent">
        <a href="#">Click Here to Display More Content</a>
 </div>
<div class="showMe" style="display:none">
        This content was hidden, but now shows up
</div>

<script>
    $('.expandContent').click(function(){
        $('.showMe').show();
    });
</script>

jsFiddle: http://jsfiddle.net/Q2wfM/

फिर भी एक तीसरा तरीका slideTogglejQuery की विधि का उपयोग करना है जो कुछ प्रभावों के लिए अनुमति देता है। जैसे $('#showMe').slideToggle('slow');कि छिपी हुई डिव को धीरे-धीरे प्रदर्शित करेगा।


मान लीजिए कि उसके पास पृष्ठ पर इन showMe तत्वों में से एक से अधिक है? याद रखें, वह इनमें से एक सूची बनाने के लिए लूप के लिए उपयोग कर रहा है, इसलिए कक्षा = "showMe" को लक्षित करना केवल उस तत्व के पहले उदाहरण को प्रभावित करेगा। मेरा सुझाव एक क्लिक के संबंध में showMe तत्व का संदर्भ देना है। तब यह एक अच्छा समाधान होगा। उम्मीद है की यह मदद करेगा! :)
jmort253

सही है, लेकिन वह <li>तत्वों की एक श्रृंखला में सूची बनाने के लिए लूप का उपयोग कर रहा है , न कि डिव्स के लिए। किसी भी तरह से, वह बस तत्व आईडी का उपयोग कर सकता है फिर उसे छिपाने के लिए।
माइकल हॉकिन्स

आप उप-वर्गों के बारे में सोच रहे हैं और भूल रहे हैं कि इनमें से अधिक होगा। प्रत्येक अनुभाग एक उपधारा में झूठे तत्वों से आबाद है । "यह सूची एक फ़ॉरच लूप द्वारा बनाई गई है जो मेरे डेटाबेस से जानकारी के साथ बनाता है। प्रत्येक आइटम विभिन्न वर्गों के साथ एक कंटेनर है, इसलिए यह 1, 2, 3 जैसी सूची नहीं है ... आदि मैं जानकारी के साथ दोहराए जाने वाले अनुभागों को सूचीबद्ध कर रहा हूं। । प्रत्येक अनुभाग में, एक उपधारा है। " संक्षेप में, उसने केवल आपको केवल एक ही खंड दिखाया, भले ही अधिक होगा।
jmort253

6

आप पैनल / डिव विस्तार या पतन बनाने के लिए लिंक पर क्लिक करने पर इस सरल जावास्क्रिप्ट विधि पर एक नज़र डालना चाहते हैं।

<script language="javascript"> 
function toggle(elementId) {
    var ele = document.getElementById(elementId);
    if(ele.style.display == "block") {
            ele.style.display = "none";
    }
    else {
        ele.style.display = "block";
    }
} 
</script>

आप div आईडी पास कर सकते हैं और यह 'कोई नहीं' या 'ब्लॉक' डिस्प्ले के बीच टॉगल करेगा।

Snip2code पर मूल स्रोत - HTML में एक div कैसे संक्षिप्त करें


6

यहां कई समस्याएं हैं

मैंने आपके लिए काम करने वाली एक फिडेल सेट की है: http://jsfiddle.net/w9kSU/

$('.majorpointslegend').click(function(){
    if($(this).text()=='Expand'){
        $('#mylist').show();
        $(this).text('Colapse');
    }else{
        $('#mylist').hide();
        $(this).text('Expand');
    }
});

3

jquery का प्रयास करें,

  <div>
        <a href="#" class="majorpoints" onclick="majorpointsexpand(" + $('.majorpointslegend').html() + ")"/>
        <legend class="majorpointslegend">Expand</legend>
        <div id="data" style="display:none" >
            <ul>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>


function majorpointsexpand(expand)
    {
        if (expand == "Expand")
            {
                $('#data').css("display","inherit");
                $(".majorpointslegend").html("Collapse");
            }
        else
            {
                $('#data').css("display","none");
                $(".majorpointslegend").html("Expand");
            }
    }

3

यहाँ एनीमेशन का एक उदाहरण है एक विवरण के विस्तार के साथ एक कर्मचारी सूची।

<html>
  <head>
    <style>
      .staff {            margin:10px 0;}
      .staff-block{       float: left; width:48%; padding-left: 10px; padding-bottom: 10px;}
      .staff-title{       font-family: Verdana, Tahoma, Arial, Serif; background-color: #1162c5; color: white; padding:4px; border: solid 1px #2e3d7a; border-top-left-radius:3px; border-top-right-radius: 6px; font-weight: bold;}
      .staff-name {       font-family: Myriad Web Pro; font-size: 11pt; line-height:30px; padding: 0 10px;}
      .staff-name:hover { background-color: silver !important; cursor: pointer;}
      .staff-section {    display:inline-block; padding-left: 10px;}
      .staff-desc {       font-family: Myriad Web Pro; height: 0px; padding: 3px; overflow:hidden; background-color:#def; display: block; border: solid 1px silver;}
      .staff-desc p {     text-align: justify; margin-top: 5px;}
      .staff-desc img {   margin: 5px 10px 5px 5px; float:left; height: 185px; }
    </style>
  </head>
<body>
<!-- START STAFF SECTION -->
<div class="staff">
  <div class="staff-block">
    <div  class="staff-title">Staff</div>
    <div class="staff-section">
        <div class="staff-name">Maria Beavis</div>
        <div class="staff-desc">
          <p><img src="http://www.craigmarlatt.com/canada/images/security&defence/coulombe.jpg" />Maria earned a Bachelor of Commerce degree from McGill University in 2006 with concentrations in Finance and International Business. She has completed her wealth Management Essentials course with the Canadian Securities Institute and has worked in the industry since 2007.</p>
        </div>
        <div class="staff-name">Diana Smitt</div>
        <div class="staff-desc">
          <p><img src="http://www.craigmarlatt.com/canada/images/security&defence/coulombe.jpg" />Diana joined the Diana Smitt Group to help contribute to its ongoing commitment to provide superior investement advice and exceptional service. She has a Bachelor of Commerce degree from the John Molson School of Business with a major in Finance and has been continuing her education by completing courses.</p>
        </div>
        <div class="staff-name">Mike Ford</div>
        <div class="staff-desc">
          <p><img src="http://www.craigmarlatt.com/canada/images/security&defence/coulombe.jpg" />Mike: A graduate of École des hautes études commerciales (HEC Montreal), Guillaume holds the Chartered Investment Management designation (CIM). After having been active in the financial services industry for 4 years at a leading competitor he joined the Mike Ford Group.</p>
        </div>
    </div>
  </div>

  <div class="staff-block">
    <div  class="staff-title">Technical Advisors</div>
    <div class="staff-section">
        <div class="staff-name">TA Elvira Bett</div>
        <div class="staff-desc">
          <p><img src="http://www.craigmarlatt.com/canada/images/security&defence/coulombe.jpg" />Elvira has completed her wealth Management Essentials course with the Canadian Securities Institute and has worked in the industry since 2007. Laura works directly with Caroline Hild, aiding in revising client portfolios, maintaining investment objectives, and executing client trades.</p>
        </div>
        <div class="staff-name">TA Sonya Rosman</div>
        <div class="staff-desc">
          <p><img src="http://www.craigmarlatt.com/canada/images/security&defence/coulombe.jpg" />Sonya has a Bachelor of Commerce degree from the John Molson School of Business with a major in Finance and has been continuing her education by completing courses through the Canadian Securities Institute. She recently completed her Wealth Management Essentials course and became an Investment Associate.</p>
        </div>
        <div class="staff-name">TA Tim Herson</div>
        <div class="staff-desc">
          <p><img src="http://www.craigmarlatt.com/canada/images/security&defence/coulombe.jpg" />Tim joined his father&#8217;s group in order to continue advising affluent families in Quebec. He is currently President of the Mike Ford Professionals Association and a member of various other organisations.</p>
        </div>
    </div>
  </div>
</div>
<!-- STOP STAFF SECTION -->

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<script language="javascript"><!--
//<![CDATA[
$('.staff-name').hover(function() {
    $(this).toggleClass('hover');
});
var lastItem;
    $('.staff-name').click(function(currentItem) {
        var currentItem = $(this);
      if ($(this).next().height() == 0) {
          $(lastItem).css({'font-weight':'normal'});
          $(lastItem).next().animate({height: '0px'},400,'swing');
          $(this).css({'font-weight':'bold'});
          $(this).next().animate({height: '300px',opacity: 1},400,'swing');
      } else {
          $(this).css({'font-weight':'normal'});
          $(this).next().animate({height: '0px',opacity: 1},400,'swing');
      }
      lastItem = $(this);
    });
//]]>
--></script>

</body></html>

बेला


3

toggle() JQuery फ़ंक्शन पर एक नज़र डालें :

http://api.jquery.com/toggle/

इसके अलावा, innerHTML jQuery फंक्शन है .html()


1
नमस्कार, स्टैक ओवरफ्लो में आपका स्वागत है! आपको एक उदाहरण दिखाना चाहिए ताकि आप उत्तर दें कि यह अधिक पूर्ण है। यदि लिंक को तोड़ना था, तो आपका उत्तर भविष्य के आगंतुकों के लिए उपयोगी होगा। सौभाग्य! :)
jmort253

क्या आप या तो एक उदाहरण जोड़ने के लिए संपादित कर सकते हैं या एक टिप्पणी के रूप में इसे जोड़ सकते हैं। धन्यवाद।
JGallardo

2

चूंकि आपके पास पृष्ठ पर jQuery है, आप उस onclickविशेषता और majorpointsexpandफ़ंक्शन को हटा सकते हैं । निम्न स्क्रिप्ट को आप पृष्ठ के नीचे या, अधिमानतः, एक .js फ़ाइल में जोड़ें:

$(function(){

  $('.majorpointslegend').click(function(){
    $(this).next().toggle().text( $(this).is(':visible')?'Collapse':'Expand' );
  });

});

यह समाधान आपके HTML के साथ काम करता है, लेकिन यह वास्तव में बहुत मजबूत जवाब नहीं है। यदि आप अपना fieldsetलेआउट बदलते हैं , तो यह उसे तोड़ सकता है। मेरा सुझाव है कि आप classउस छिपे हुए div में एक विशेषता डालें , जैसे class="majorpointsdetail"और इस कोड का उपयोग करें:

$(function(){

  $('.majorpoints').on('click', '.majorpointslegend', function(event){
    $(event.currentTarget).find('.majorpointsdetail').toggle();
    $(this).text( $(this).is(':visible')?'Collapse':'Expand' );
  });

});

अवलोकन: </fieldset>आपके प्रश्न में कोई समापन टैग नहीं है इसलिए मैं मान रहा हूं कि छिपा हुआ विभाजन फ़ील्ड के अंदर है।


तुम सही हो। एक समापन फ़ील्डसेट है, लेकिन मैंने इसे अपने प्रश्न में याद किया। यह बंद होने के तुरंत बाद आता है </ div> और समापन से पहले बाहरी </ div>
रयान मोर्टेंसन

1

यदि आपने डेटा-भूमिका का उपयोग किया है जैसे कि बंधनेवाला

    <div id="selector" data-role="collapsible" data-collapsed="true">
    html......
    </div>

तब यह विस्तारित div को बंद कर देगा

    $("#selector").collapsible().collapsible("collapse");   

1

जेड फोस्टर के Readmore.js को देखें लाइब्रेरी देखें।

यह उपयोग के रूप में सरल है:

$(document).ready(function() {
  $('article').readmore({collapsedHeight: 100});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script src="https://fastcdn.org/Readmore.js/2.1.0/readmore.min.js" type="text/javascript"></script>

<article>
  <p>From this distant vantage point, the Earth might not seem of any particular interest. But for us, it's different. Consider again that dot. That's here. That's home. That's us. On it everyone you love, everyone you know, everyone you ever heard of, every human being who ever was, lived out their lives. The aggregate of our joy and suffering, thousands of confident religions, ideologies, and economic doctrines, every hunter and forager, every hero and coward, every creator and destroyer of civilization, every king and peasant, every young couple in love, every mother and father, hopeful child, inventor and explorer, every teacher of morals, every corrupt politician, every "superstar," every "supreme leader," every saint and sinner in the history of our species lived there – on a mote of dust suspended in a sunbeam.</p>

  <p>Space, the final frontier. These are the voyages of the starship Enterprise. Its five year mission: to explore strange new worlds, to seek out new life and new civilizations, to boldly go where no man has gone before!</p>

  <p>Here's how it is: Earth got used up, so we terraformed a whole new galaxy of Earths, some rich and flush with the new technologies, some not so much. Central Planets, them was formed the Alliance, waged war to bring everyone under their rule; a few idiots tried to fight it, among them myself. I'm Malcolm Reynolds, captain of Serenity. Got a good crew: fighters, pilot, mechanic. We even picked up a preacher, and a bona fide companion. There's a doctor, too, took his genius sister out of some Alliance camp, so they're keeping a low profile. You got a job, we can do it, don't much care what it is.</p>

  <p>Space, the final frontier. These are the voyages of the starship Enterprise. Its five year mission: to explore strange new worlds, to seek out new life and new civilizations, to boldly go where no man has gone before!</p>
</article>

अपने विजेट को कॉन्फ़िगर करने के लिए यहां उपलब्ध विकल्प हैं:

{
  speed: 100,
  collapsedHeight: 200,
  heightMargin: 16,
  moreLink: '<a href="#">Read More</a>',
  lessLink: '<a href="#">Close</a>',
  embedCSS: true,
  blockCSS: 'display: block; width: 100%;',
  startOpen: false,

  // callbacks
  blockProcessed: function() {},
  beforeToggle: function() {},
  afterToggle: function() {}
},

इसका उपयोग कर सकते हैं जैसे:

$('article').readmore({
  collapsedHeight: 100,
  moreLink: '<a href="#" class="you-can-also-add-classes-here">Continue reading...</a>',
});

मुझे उम्मीद है यह मदद करेगा।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.