javascript - Hide and then append data with jQuery - Stack Overflow

By Clicking a link .link, I want to append a data data in a div .list. I want to set it initially hidde

By Clicking a link .link, I want to append a data data in a div .list. I want to set it initially hidden and then show it (to create a fading effect). I am trying following but it does not seem to do anything (neither I get any error). What's wrong with it?

HTML:

<div class="wrap">
    <div class="box">
        <div class="list">

        </div>
    </div>
    <a href="#" class="link">click me</a>
</div>

JS:

$('.link').click(function(e) {
    var wrap = $(this).closest('.wrap').find('.list' );
    var mydata = "test data";

    $(mydata).hide(1000, function(){                        
        $(wrap).append($(mydata));
        $(mydata).show(6000);
    });

});

JSFiddle Demo: /

By Clicking a link .link, I want to append a data data in a div .list. I want to set it initially hidden and then show it (to create a fading effect). I am trying following but it does not seem to do anything (neither I get any error). What's wrong with it?

HTML:

<div class="wrap">
    <div class="box">
        <div class="list">

        </div>
    </div>
    <a href="#" class="link">click me</a>
</div>

JS:

$('.link').click(function(e) {
    var wrap = $(this).closest('.wrap').find('.list' );
    var mydata = "test data";

    $(mydata).hide(1000, function(){                        
        $(wrap).append($(mydata));
        $(mydata).show(6000);
    });

});

JSFiddle Demo: http://jsfiddle/9z06nvh7/

Share Improve this question asked Sep 30, 2015 at 10:50 samsam 1,0873 gold badges12 silver badges22 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

mydata is a text, so when it is passed to jQuery it will be considered as a selector thus no element will be returned and the hide call will not do anything after that.

You need to hide the wrap element and show it

$('.link').click(function(e) {
  var wrap = $(this).closest('.wrap').find('.list');
  var mydata = "test data";
  wrap.hide().html(mydata).show(6000);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
  <div class="box">
    <div class="list"></div>
  </div>
  <a href="#" class="link">click me</a>
</div>

You have to use wrap instead mydata for hide and show

$('.link').click(function(e) {
    var wrap = $(this).closest('.wrap').find('.list' );
    var mydata = "test data";

    $(wrap).hide(1000, function(){                        
        $(wrap).append($(mydata));
        $(wrap).show(6000);
    });

});

JSFIDDLE DEMO

Try this : You are hiding and showing mydata instead of wrap which is a list div, see below code

$('.link').click(function(e) {
    var wrap = $(this).closest('.wrap').find('.list' );
    var mydata = "test data";

    $(wrap).hide(1000, function(){                      
        $(wrap).append(mydata);
        $(wrap).show(6000);
    });

});

JSFiddle Demo

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744314915a4568132.html

相关推荐

  • javascript - Hide and then append data with jQuery - Stack Overflow

    By Clicking a link .link, I want to append a data data in a div .list. I want to set it initially hidde

    7天前
    80

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信