[英]Javascript show/hide - I don't want it to hide the entire element
This is probably a fairly easy question, but I'm new to JavaScript and jquery.... 这可能是一个相当简单的问题,但是我是JavaScript和jquery的新手。
I have a website with a basic show/hide toggle. 我有一个带有基本显示/隐藏切换的网站。 The show/hide function I'm using is here: http://andylangton.co.uk/articles/javascript/jquery-show-hide-multiple-elements/
我正在使用的show / hide功能在这里: http : //andylangton.co.uk/articles/javascript/jquery-show-hide-multiple-elements/
So here's my question..... I would really like the first 5-10 words of the toggled section to always be visible. 所以这是我的问题.....我真的希望切换部分的前5-10个字始终可见。 Is there some way I can change it so that it doesn't hide the entire element, but hides all but the first few words of the element?
有什么办法可以更改它,以便它不隐藏整个元素,而是隐藏除元素的前几个单词以外的所有单词?
Here's a screenshot of what I would like it to do: http://answers.alchemycs.com/mobile/images/capture.jpg 这是我想要执行的操作的屏幕截图: http : //answers.alchemycs.com/mobile/images/capture.jpg
There are many different implementation possibilities: 有许多不同的实现可能性:
Here's a working example of option 1: http://jsfiddle.net/jfriend00/CTzsP/ . 这是选项1的工作示例: http : //jsfiddle.net/jfriend00/CTzsP/ 。
You'd need to either: 您需要:
The last is a bit more customizable; 最后一个更具可定制性。 using two separate elements allows trivial games to be played (showing an image, for example, like a little curly arrow) without modifying adding/removing DOM elements.
使用两个单独的元素可以进行琐碎的游戏(例如,显示图像,例如,一个小的卷曲箭头),而无需修改添加/删除DOM元素。
I tend towards the last because it's simple and obvious, but that's a personal preference, and really isn't as true as it used to be. 我倾向于最后一种方法,因为它简单明了,但这是个人喜好,实际上并不像过去那样正确。
You can do some plugin authoring ,I did a sample demo here ,based on your screenshot 您可以进行一些插件创作 ,根据您的屏幕截图,我在此处做了一个示例演示
<div class="toggle">ShowHide</div>
<div class="content">some content some content some content some content some content <br/> some content some content some content </div>
<div class="toggle">ShowHide</div>
<div class="content">some content some content some content some content some content <br/> some content some content some content </div>
here is javascript/jquery code 这是javascript / jquery代码
jQuery.fn.myToggle = function(selector, count) {
var methods = {
toggle: function(selector, count) {
if ($(selector).is(':visible')) {
var span = $('<span>');
span.text($(selector).text().substr(0, count) + "...");
span.insertAfter($(selector));
$(selector).hide();
}
else {
$(selector).show();
$(selector).next('span').hide();
}
}
};
$(this).each(function() {
methods.toggle($(this).next(selector), count);
$(this).click(function(evt) {
methods.toggle($(this).next(selector), count);
});
});
};
$(function() {
$('.toggle').myToggle('.content', 3);
});
Here is a solution using css properties only instead of mangling the dom. 这是仅使用css属性而不是破坏dom的解决方案。
http://jsfiddle.net/AYre3/4/ http://jsfiddle.net/AYre3/4/
Now if you want some sort of animation happening as well you'll probably need to do a bit of measurement along the way. 现在,如果您还希望发生某种动画,则可能需要在此过程中进行一些测量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.