Home >Web Front-end >JS Tutorial >jquery special effects click to show and hide full text_jquery

jquery special effects click to show and hide full text_jquery

WBOY
WBOYOriginal
2016-05-16 15:26:341431browse

The example in this article describes the special effects of jquery clicking to display and hide the full text. Here, jquery is used to implement the expansion and hiding special effects. Click to view the full text and the content will expand. Click to collapse the full text and the content will shrink. Share it with everyone for your reference. The details are as follows:

The screenshot of the running effect is as follows:

The specific code is as follows

1. Let’s take a look at the main framework program:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>点击查看全文</title>
    <link type="text/css" rel="stylesheet" href="css/layout.css" />
    <script type="text/javascript" src="js/jquery-1.11.3.js"></script>
    <script type="text/javascript" src="js/layout.js"></script>
  </head>
  <body>
    <div class="showAll">
      <p class="title">
        一则励志故事              
      </p>
      <p class="author">
         作者:来自网络 发表时间:2014-3-1
      </p>
      <p>
        彼得·韩德先生现任卡内基公司 (Dale Carnegie & Associates) 总裁及首席执行官。卡内基公司为训练界中的翘楚,在全世界85个国家有160个分支机构。 
        除此之外,彼得先生还是数家大公司的董事,作为一个培训别人怎样获得成功的专业机构的总裁,他是怎样获得成功的呢?日前,记者在北京的东方君悦大酒店采访了
        这位CEO,听他讲述了自己是怎样获得成功的故事。 
        彼得先生通过一个故事讲了他对成功的理解。他说他在五岁时因为生病去看医生,当时病痛...
        <a class="showContent" href="javascript:void(0);">查看全文</a>
      </p>
      <div class="content">
        彼得·韩德先生现任卡内基公司 (Dale Carnegie & Associates) 总裁及首席执行官。卡内基公司为训练界中的翘楚,在全世界85个国家有160个分支机构。
         除此之外,彼得先生还是数家大公司的董事,作为一个培训别人怎样获得成功的专业机构的总裁,他是怎样获得成功的呢?日前,记者在北京的东方君悦大酒店采访了
         这位CEO,听他讲述了自己是怎样获得成功的故事。 
         彼得先生通过一个故事讲了他对成功的理解。他说他在五岁时因为生病去看医生,当时病痛使他很难受,医生当时问他,你最想要的是什么,彼得先生对医生说,我想
         要快乐,医生说,那你快乐就是了,结果他真的很快乐。所以彼得先生说,有许多人想追求成功,也有许多人问他,怎样才能尽快地获得成功。他认为,这要先看你对
         成功的定义是什么?你的成功定义若是家庭和谐,那你就应想办法跟家庭成员更多地沟通,为此付出更多的时间,并在提升家庭成员的和谐之中也提升自己处理家庭问
         题的能力。 
         彼得先生说:“我对成功的定义是快乐,我不会做我不喜欢的事和不喜欢的工作。中国的一句俗语说‘人在屋檐下,不得不低头',我不喜欢那样的境况,我也不会那样
         做。由于我认为快乐就是成功,所以说,我在5岁时就已经很成功了。” 
         <a class="hideContent" href="javascript:void(0);">收起全文</a>
      </div>   
    </div>    
  </body>
</html>

What needs to be noted in the above program layout is that the content in the div named "content" needs to be consistent with the text before the ellipsis ". . .", that is, repeating a paragraph of text.

Reason: Because the text before the ellipsis ". . ." will be hidden when the "View Full Text" button is clicked. If the text before the ellipsis ". . ." is not hidden, As for text, the ". . ." will not disappear after clicking the "View Full Text" button, so the article cannot be read smoothly~~~~

2. Let’s take a look at the style:

*{
  padding: 0;
  margin: 0;
}
.showAll{
  width: 60%;
  margin: 0 auto;
  background: #ecebeb;
  padding: 10px;
}
.showAll .title{
  font-size: 20px;
  font-weight: bold;
  color:#af0015;
}
.showAll .author{
  color: #a1a1a1;
  margin: 12px 0;
}
.showAll .content{
  display: none;   //注意这里让文字不显示
}

What needs to be noted above is that the div named "content" needs to be hidden, so that the display event can be triggered only after clicking the "View Full Text" button.
3. Jquery program:

$(document).ready(function(){
  $(".showContent").click(function(){        //当“展开全文”按钮点击的时候
    $(".content").show();             //展示未完全显示的那部分内容
    $(this).parent().hide();           //此处需要注意隐藏简略说明的文字,因为原来文字里面最后有省略号,不隐藏的后果就是展开后省略号仍旧在那里
  });
  $(".hideContent").click(function(){         //当“收起全文”按钮点击的时候
    $(this).parent().hide();           //隐藏已经显示出来的文字
    $(".showContent").parent().show();       //将简略说明的文字显示出来
  });
});

I hope this article will be helpful to everyone learning jquery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn