在JavaScript中切换
Toggling in JavaScript
本文关键字:JavaScript 更新时间:2023-09-26
我有以下代码片段:
<!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>
因此,这个代码部分与toggle选项有关,其中代码的一部分将被显示,而另一部分将通过show more选项隐藏。当任何人点击它时,整个内容都会被加载。最后显示了另一个显示较少的链接。当有人点击show less时,内容应该再次压缩。但由于某种原因,我不知道文本的收缩没有被调用。合同方法未被称为
附件是jsfiddle的链接http://jsfiddle.net/rapandey/48HyG/
Show less选项不起作用
您需要事件委派:
$(".container").on('click','.contract',function () {
$(".container").animate({
height: 100
});
});
也做同样的显示更多选项:
$(document).on('click','.enlarge',function () {
//rest code
});
工作演示
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 使用php或javascript从facebook相册URL中删除多余的部分
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在JavaScript中将字符串转换为函数引用
- 模糊事件的Javascript测试
- Javascript更改图标
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何使用WCF服务和javascript表单post上传.doc文件
- javascript结合了数组和字典
- 这是什么 ==- javascript 运算符
- 从javascript创建一个列表
- 无法在通过jQuery的ajax加载的页面中执行javascript
- Javascript:selenium Web驱动程序isDisplayed()不工作
- 如何通过ajax刷新JSF填充的javascript变量
- 如何在Javascript中将JSon对象转换为数组
- Javascript生成的表单未提交
- 使用javascript将动态表从一个html页面打印到另一个html页
- 通过javascript重定向html传递php变量