2017-10-12 13 views
-1

ボタンをクリックしてテキストを拡大したり折りたたんだりするのに問題があります。私はあなたがボタンをクリックするとテキストを崩壊させることができましたが、それを元に戻すことができるようにする必要もあります。私は最初にそれを隠すようにする必要があり、ボタンをクリックするとそれが消費され、その後に再び折りたたむことができます。ここに私のコードはjQueryの展開と折りたたみのテキスト

$(document).ready(function() { 
 
    $('#btnSlideUp').click(function() { 
 
    $('#p1').slideUp(1000); 
 
    }); 
 

 
    $('#btnSlideDown').click(function() { 
 
    $('#p1').slideDown(1000); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    <button id="btnSlideUp" class="btn btn-outline-success"><h1 class="jumbotron-heading">Expand</h1></button> 
 
    <p class="lead text-muted" id="p1">Below this post you can find different articles, tips&tricks about how to find the job. You can try to contact us, and we will greatly try to answer all of your questions. You can click on "Start finding a job" and we will take you through the basics 
 
    of finding a job, from the beginning till the end if you have absolutely no excperience.</p> 
 
    <p> 
 
    <a href="contactus.html"><button type="button" class="btn btn- 
 
    outline-primary btn-lg">Contact Us</button></a> 
 
    <a href="path.html"><button type="button" class="btn btn-outline 
 
    secondary btn-lg">Start finding a job</button></a> 
 

 
    </p> 
 
</div>

答えて

1

ブール

var isDown=true; 
 

 
    $('#btnSlideUp').click(function() { 
 
     
 
     if(isDown){ 
 
     $('#p1').slideUp(1000); 
 
     isDown=false; 
 
     }else 
 
     { 
 
     $('#p1').slideDown(1000); 
 
     isDown=true; 
 
     } 
 
    
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    <button id="btnSlideUp" class="btn btn-outline-success"><h1 class="jumbotron-heading">Expand</h1></button> 
 
    <p class="lead text-muted" id="p1">Below this post you can find different articles, tips&tricks about how to find the job. You can try to contact us, and we will greatly try to answer all of your questions. You can click on "Start finding a job" and we will take you through the basics 
 
    of finding a job, from the beginning till the end if you have absolutely no excperience.</p> 
 
    <p> 
 
    <a href="contactus.html"><button type="button" class="btn btn- 
 
    outline-primary btn-lg">Contact Us</button></a> 
 
    <a href="path.html"><button type="button" class="btn btn-outline 
 
    secondary btn-lg">Start finding a job</button></a> 
 

 
    </p> 
 
</div>

+0

申し訳ありませんが、あなたの答えは私にとってはうまくいくと思いますが、簡単な質問です。ページが読み込まれたときにテキストを非表示にしてから、ユーザーがクリックするとテキストが表示されます。 – iKON1K

+0

@ iKON1Kは** CSS **プロパティ 'display:none'を使用し、ユーザーが* expand *をクリックしたときに' $( '#p1')。show() ' –

+0

を使用します。たくさん – iKON1K

3

であるあなたが代わりにjQueryのトグルメソッドを使用することができます。

$(document).ready(function(){ 
    $('#btnSlideUp').click(function(){ 
     $('#p1').toggle(1000); 
    }); 
}); 
4

問題は、ボタンのクリックイベントに2つのハンドラをバインドしていることです。ボタンをクリックすると、両方ともトリガーされますが、最初のもの(slideUp) が表示されます。

$('#btnSlideDown')は、存在しない要素を指します(少なくともあなたの例ではありません)。

これを解決する最も簡単な方法は、jQueryのslideToggle()メソッドを使用してclickイベントを処理する方法です。

$(document).ready(function() { 
 
    $('#btnSlideUp').click(function() { 
 
    $('#p1').slideToggle(1000); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    <button id="btnSlideUp" class="btn btn-outline-success"><h1 class="jumbotron-heading">Expand</h1></button> 
 
    <p class="lead text-muted" id="p1">Below this post you can find different articles, tips&tricks about how to find the job. You can try to contact us, and we will greatly try to answer all of your questions. You can click on "Start finding a job" and we will take you through the basics 
 
    of finding a job, from the beginning till the end if you have absolutely no excperience.</p> 
 
    <p> 
 
    <a href="contactus.html"><button type="button" class="btn btn- 
 
    outline-primary btn-lg">Contact Us</button></a> 
 
    <a href="path.html"><button type="button" class="btn btn-outline 
 
    secondary btn-lg">Start finding a job</button></a> 
 

 
    </p> 
 
</div>

+1

を使用して、彼はTの実際の問題ではありませんでした$( '#btnSlideDown')にバインドしようとしましたが、そのIDの要素はありませんか?あなたの解決策は+1ですが) –

+0

@ Mr.Greenwoodz私はちょうど私が2番目のハンドラのjQueryを読んでいないのを見ました。 – j08691

+0

最高のことが起こる:) –

2

これを試してみてください。

$(document).ready(function() { 
    $('#btnSlideUp').click(function() { 
    $('#p1').slideToggle(1000); 
    }); 

あなたは完全なマニュアルhere

2

jquery .slideToggle()機能を使用して読むことができます。

$(document).ready(function() { 
 
    $('#btnSlideUp').click(function() { 
 
    $('#p1').slideToggle(1000); 
 
    }); 
 

 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    <button id="btnSlideUp" class="btn btn-outline-success"><h1 class="jumbotron-heading">Expand</h1></button> 
 
    <p class="lead text-muted" id="p1">Below this post you can find different articles, tips&tricks about how to find the job. You can try to contact us, and we will greatly try to answer all of your questions. You can click on "Start finding a job" and we will take you through the basics 
 
    of finding a job, from the beginning till the end if you have absolutely no excperience.</p> 
 
    <p> 
 
    <a href="contactus.html"><button type="button" class="btn btn- 
 
    outline-primary btn-lg">Contact Us</button></a> 
 
    <a href="path.html"><button type="button" class="btn btn-outline 
 
    secondary btn-lg">Start finding a job</button></a> 
 

 
    </p> 
 
</div>

関連する問題