Replace Div Content onclick

前端 未结 6 995
名媛妹妹
名媛妹妹 2020-12-06 07:48

I know nothing about jQuery but want to achieve something really important.

I want to have a button/link that will replace the div content and if I press that button

相关标签:
6条回答
  • 2020-12-06 08:04

    Here's an approach:

    HTML:

    <div id="1">
        My Content 1
    </div>
    
    <div id="2" style="display:none;">
        My Dynamic Content
    </div>
    <button id="btnClick">Click me!</button>
    

    jQuery:

    $('#btnClick').on('click',function(){
        if($('#1').css('display')!='none'){
        $('#2').html('Here is my dynamic content').show().siblings('div').hide();
        }else if($('#2').css('display')!='none'){
            $('#1').show().siblings('div').hide();
        }
    });
    


    JsFiddle:
    http://jsfiddle.net/ha6qp7w4/
    http://jsfiddle.net/ha6qp7w4/4 <--- Commented

    0 讨论(0)
  • 2020-12-06 08:08

    EDIT: This answer was submitted before the OP's jsFiddle example was posted in question. See second answer for response to that jsFiddle.


    Here is an example of how it could work:

    Working jsFiddle Demo

    HTML:

    <div id="someDiv">
        Once upon a midnight dreary
        <br>While I pondered weak and weary
        <br>Over many a quaint and curious
        <br>Volume of forgotten lore.
    </div>
    Type new text here:<br>
    <input type="text" id="replacementtext" />
    <input type="button" id="mybutt" value="Swap" />
    
    <input type="hidden" id="vault" />
    

    javascript/jQuery:

    //Declare persistent vars outside function
    var savText, newText, myState = 0;
    
    $('#mybutt').click(function(){
        if (myState==0){
            savText = $('#someDiv').html(); //save poem data from DIV
            newText = $('#replacementtext').val(); //save data from input field
            $('#replacementtext').val(''); //clear input field
            $('#someDiv').html( newText ); //replace poem with insert field data
            myState = 1; //remember swap has been done once
        } else {
            $('#someDiv').html(savText);
            $('#replacementtext').val(newText); //replace contents
            myState = 0;
        }
    });
    
    0 讨论(0)
  • 2020-12-06 08:09

    A simple addClass and removeClass will do the trick on what you need..

    $('#change').on('click', function() { 
      $('div').each(function() { 
        if($(this).hasClass('active')) { 
            $(this).removeClass('active');
        } else { 
            $(this).addClass('active');
        }
    });
    

    });

    Seee fiddle

    I recommend you to learn jquery first before using.

    0 讨论(0)
  • 2020-12-06 08:09

    Working from your jsFiddle example:

    The jsFiddle was fine, but you were missing semi-colons at the end of the event.preventDefault() statements.

    This works: Revised jsFiddle

    jQuery(document).ready(function() {
        jQuery(".rec1").click(function(event) {
            event.preventDefault();
            jQuery('#rec-box').html(jQuery(this).next().html()); 
        });
        jQuery(".rec2").click(function(event) {
            event.preventDefault();
            jQuery('#rec-box2').html(jQuery(this).next().html()); 
        });
    });
    
    0 讨论(0)
  • 2020-12-06 08:21

    A Third Answer

    Sorry, maybe I have it correct this time...

    jsFiddle Demo

    var savedBox1, savedBox2, state1=0, state2=0;
    
    jQuery(document).ready(function() {
        jQuery(".rec1").click(function() {
            if (state1==0){
                savedBox1 = jQuery('#rec-box').html();
                jQuery('#rec-box').html(jQuery(this).next().html()); 
                state1 = 1;
            }else{
                jQuery('#rec-box').html(savedBox1); 
                state1 = 0;
            }
        });
    
        jQuery(".rec2").click(function() {
            if (state1==0){
                savedBox2 = jQuery('#rec-box2').html();
                jQuery('#rec-box2').html(jQuery(this).next().html()); 
                state2 = 1;
            }else{
                jQuery('#rec-box2').html(savedBox2); 
                state2 = 0;
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-06 08:21

    Try This:

    I think that you want something like this.

    HTML:

    <div id="1">
        My Content 1
    </div>
    
    <div id="2" style="display:none;">
        My Dynamic Content
    </div>
    <button id="btnClick">Click me!</button>
    

    jQuery:

    $('#btnClick').on('click',function(){
    if($('#1').css('display')!='none'){
    $('#2').show().siblings('div').hide();
    }else if($('#2').css('display')!='none'){
        $('#1').show().siblings('div').hide();
    }
    });
    


    JsFiddle:
    http://jsfiddle.net/ha6qp7w4/1113/ <--- see this I hope You want something like this.

    0 讨论(0)
提交回复
热议问题