简体   繁体   English

用Javascript切换

[英]Toggling in Javascript

I have the following code snippet 我有以下代码片段

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

<script src="C:\Users\rapandey\Documents\Visual Studio 2012\Projects\jquery-1.11.0.min.js">
</script>
<script>
    $(document).ready(function () {
        $('.container .text').each(function () {
            if ($(this).height() > 100) {
                $(this).parent().addClass('container-cut').append('<div class="enlarge">... (Show More)</div>');
            }

        });

        $(".enlarge").click(function () {
           // $(this).remove();

            text_height = $(".text").height();

            $(".container").animate({
                height: text_height
            });

            $('.container .text').each(function () {
                if ($(this).height() > 100) {
                    $(this).parent().addClass('container-cut').append('<div class="contract">... (Show Less)</div>');
                }

            });
        });


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

            $(".container").animate({
                height: 100
            });

        });
    });
    </script>

<style type="text/css">
   .container{
    width:200px;
    border:1px solid #cacaca;
    font-size:12px;
    overflow:hidden;
    position:relative;
    margin:auto;
    padding:10px;
}
.container-cut { height: 100px }
.enlarge{
    position:absolute;
    background:#fff;
    bottom:-1px;
    right:10px;
    width:80px;
    color:blue;
}
  .contract{
    position:absolute;
    background:#fff;
    bottom:-1px;
    right:10px;
    width:80px;
    color:blue;
}

</style>
</head>

<body>
<div class="container">
    <div class="text">
Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then
    </div>
</div>
</body>
</html>

So this code part is related to toggle option, where a part of the code will be shown and some part will be hidden with the option of show more. 因此,此代码部分与切换选项相关,在该选项中,将显示部分代码,而部分部分将与显示更多选项一起隐藏。 When any one clicks on it the whole contents gets loaded. 当任何人单击它时,整个内容将被加载。 Finally another link is shown up which is show less. 最后,显示了另一个链接,该链接显示较少。 when someone clicks on show less the content again should compress. 当有人点击显示较少时,内容应再次压缩。 But I have no clue for some reason contraction of text is not getting called. 但是由于某种原因,我不知道文本压缩没有得到调用。 Contract method is not getting called 没有调用合同方法

Attached is the link for jsfiddle http://jsfiddle.net/rapandey/48HyG/ 附件是jsfiddle的链接http://jsfiddle.net/rapandey/48HyG/

Show less option is not working 显示较少的选项不起作用

You need event delegation: 您需要事件委托:

 $(".container").on('click','.contract',function () {
        $(".container").animate({
            height: 100
        });
 });

also do the same for show more option: 也可以显示更多选项:

 $(document).on('click','.enlarge',function () {
   //rest code
 });

Working Demo 工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM