2016-07-15 7 views
0

私はjqueryを使用してボタンをクリックするとテキストを表示します。 別の場所にある同じページに、別のテキストを表示する別のボタンが必要です。 私のソリューションは現在、それぞれに1つは、このjquery hideと別の場所に表示

<a class="pure-button pure-button-primary" type='button' id='hideshow1' value='hide/show1' >Show me1 ! </a> 
<div id='content1' style="display:none"> hide and show 1 </div> 

<script> 
jQuery(document).ready(function(){ 
     jQuery('#hideshow1').on('click', function(event) {   
      jQuery('#content1').toggle('hide'); 
     }); 
    }); 
</script> 

そして、このすべてのコピーのようにDIV複数のスクリプトを作成することですが、その後「hideshow2」と「content2」

で今私も三hideshow3を必要とするが、私はこのスクリプトを1つのスクリプトでよりスマートにする方法を理解できません。

+0

コンテンツを持つ要素は、クリックした要素の隣に常に表示されますか?そうであれば 'jQuery( '。pure-button')を実行することができます( 'click'、function(){jQuery(this).next()。toggle( 'hide');})'。 – putvande

答えて

3

代わりにクラス名を使用してください。それは何度も何度も同じことを書くのあなたに苦痛を与えることはありません:テキストを表示する

jQuery(document).ready(function() { 
 
    jQuery('.hideshow').on('click', function(event) { 
 
    jQuery(this).next('.content').toggle('hide'); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a class="pure-button pure-button-primary hideshow" type='button' value='hide/show1'>Show me1 ! </a> 
 
<div class='content' style="display:none">hide and show 1</div><br> 
 
<a class="pure-button pure-button-primary hideshow" type='button' value='hide/show1'>Show me2 ! </a> 
 
<div class='content' style="display:none">hide and show 2</div><br> 
 
<a class="pure-button pure-button-primary hideshow" type='button' value='hide/show1'>Show me3 ! </a> 
 
<div class='content' style="display:none">hide and show 3</div>

+0

彼らはお互いの隣にいる可能性は非常に低いですが、確かに質問は、構造ではなくhtmlを表示するこの方法がありますか? –

+1

あなたは正しいですが、私はちょっとした変更でマークアップに基づいて答えたいと思っていました。 – Jai

+0

これは素晴らしいですね。私はこのように試してみる。あなたのお返事ありがとうございます –

0

使用jqueryのをあなたがそれらをリンクすることができ、ボタンが

をクリックされたときに、 data-属性を使用します。この方法では、要素が互いに近くにあり、スクリプトを書き直す必要なしに将来移動できるかどうかは関係ありません。

<a class='hideshow' data-hideshow="1">Show me 1!</a> 
<a class='hideshow' data-hideshow="2">Show me 2!</a> 

<div class='content' data-hideshow="1" style="display:none">hide and show 1 </div> 
<div class='content' data-hideshow="2" style="display:none">hide and show 2 </div> 

次に、あなたのスクリプトはクラスと属性で動作します:

$(".hideshow").click(function() { 
    var hideshow = $(this).data("hideshow"); 

    // Optional hide all other .content (not clear if this is required or not) 
    $(".content").hide(); 

    // Show the relevant .content 
    $(".content[data-hideshow=" + hideshow + "]").show(); 
}); 

.hide()/ショー()の呼び出しは(.NOTと組み合わせることにより、より効率的にすることができる)、その他のコール。しかし、これは概念を示しています。

+0

これは、OPがやっているように、「トグル」をしません。たとえば、最初のリンクをクリックすることで 'hide and show 1'を隠すことはできません。 – putvande

+0

@putvandeあなたは正しいですが、OPの元のコードとは一致しませんが、OPの質問と一致します - "*ボタンをクリックするとテキストを表示するためにjqueryを使用する"という質問のテキストが表示されます。リンク「* show me 1 *」はどちらも「ショー」であり、トグルではありません。 –

1

リンクtype属性でとその悪い...

$(document).ready(function(){ 
 
     $('a[id^="hideshow"]').on('click', function(event) {   
 
      $(this).next().toggle('hide'); 
 
     }); 
 
    });
a{ display: block; cursor: pointer;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a class="pure-button pure-button-primary" id='hideshow1'>Show me1 ! </a> 
 
<div id='content1' style="display:none"> hide and show 1 </div> 
 

 
<a class="pure-button pure-button-primary" id='hideshow2'>Show me2 ! </a> 
 
<div id='content2' style="display:none"> hide and show 2 </div> 
 

 
<a class="pure-button pure-button-primary" id='hideshow3'>Show me3 ! </a> 
 
<div id='content3' style="display:none"> hide and show 3 </div>

+0

彼らはお互いの隣にいる可能性は非常に低いですが、確かに質問は、構造ではなくhtmlを表示するこの方法がありますか? –

+0

著者の構造のためのこの解決策 –

+0

私は6つの答えを得た、ただ1つを投票することができます。しかし、これは私のために素晴らしいと私が探していた。あなたの努力に感謝します。 –

0

に価値を私はあなたがボタンのdivボタンのdivボタンのdivのような構造を持つようにしたいと思います。

<a class="pure-button pure-button-primary" type='button' id='hideshow1' value='hide/show1' >Show me1 ! </a> 
<div id='content1' style="display:none"> hide and show 1 </div> 
<a class="pure-button pure-button-primary" type='button' id='hideshow2' value='hide/show2' >Show me2 ! </a> 
<div id='content2' style="display:none"> hide and show 2 </div> 
<a class="pure-button pure-button-primary" type='button' id='hideshow3' value='hide/show3' >Show me3 ! </a> 
<div id='content3' style="display:none"> hide and show 3 </div> 
<a class="pure-button pure-button-primary" type='button' id='hideshow4' value='hide/show4' >Show me4 ! </a> 
<div id='content4' style="display:none"> hide and show 4 </div> 

ORあなたのdivが直接、ボタンの後、あなたはそうのようなdata-targetを使用することができていない場合:そう

jQuery(document).ready(function(){ 
    jQuery(".pure-button").on('click', function(event) {   
    var show = $(this).next("div") 
     $(show).toggle('hide'); 
    }); 
}); 

HTMLこの

jsfiddle

JQを使用:jsfiddle

HTML:

<a class="pure-button pure-button-primary" type='button' id='hideshow1' data-target="#content1" value='hide/show1' >Show me1 ! </a> 
<a class="pure-button pure-button-primary" type='button' id='hideshow2' data-target="#content2" value='hide/show2' >Show me2 ! </a> 
<a class="pure-button pure-button-primary" type='button' id='hideshow3' data-target="#content3" value='hide/show3' >Show me3 ! </a> 
<a class="pure-button pure-button-primary" type='button' id='hideshow4' data-target="#content4" value='hide/show4' >Show me4 ! </a> 

<div id='content1' style="display:none"> hide and show 1 </div> 
<div id='content2' style="display:none"> hide and show 2 </div> 
<div id='content3' style="display:none"> hide and show 3 </div> 
<div id='content4' style="display:none"> hide and show 4 </div> 

JQ:

jQuery(document).ready(function(){ 
    jQuery(".pure-button").click(function() {   
    var show = $(this).data("target") 
     $(show).toggle('hide'); 
    }); 
}); 
+0

なぜ '.each'ですか? 'jQuery( '。pure-button')。on( 'click' ... 'はうまく動作します。 – putvande

0

なぜ要素を選択するためにクラスを追加しますか?適切な方法でjQueryセレクタを使用する方がはるかに簡単です。

異なるアプローチ、 "#contentx" の要素を考慮すると、連続していないことができます:

$(function() { 
 
    // get all elements with the id starting with hideshow 
 
    $('[id^="hideshow"]').on('click', function(event) { 
 
    // find all content elements ending with the same 
 
    // substring of the clicked element 
 
    $('#content' + this.id.substr(8)).toggle('hide'); 
 
    }); 
 
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> 
 

 
<a class="pure-button pure-button-primary" type='button' id='hideshow1' value='hide/show1' >Show me1 ! </a> 
 
<div id='content1' style="display:none"> hide and show 1 </div> 
 

 
<a class="pure-button pure-button-primary" type='button' id='hideshow2' value='hide/show1' >Show me1 ! </a> 
 
<div id='content2' style="display:none"> hide and show 1 </div> 
 

 
<a class="pure-button pure-button-primary" type='button' id='hideshow3' value='hide/show1' >Show me1 ! </a> 
 
<div id='content3' style="display:none"> hide and show 1 </div>

0
$(document).ready(function() { 
    $('.pure-button').on('click', function(event) { 
    var id = $(this).attr('id'); 
    id = id.substring(8); 
    $('#content' + id).toggle('hide'); 
    }); 
}); 

私は、そのコードはあなたを助けるだろうと思い、クリックされたボタンの場合と表示ボックスには同じ数字が付きます

関連する問題