简体   繁体   English

Javascript显示/隐藏-我不希望它隐藏整个元素

[英]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: 有许多不同的实现可能性:

  1. You can divide the contents up into the first part and the second part (two separate spans or divs inside your main object) and hide only the child object that represents the second part, not hide the parent object. 您可以将内容分为第一部分和第二部分(主对象内的两个单独的span或div),并且仅隐藏代表第二部分的子对象,而不隐藏父对象。
  2. Rather than hide the object at all, you can set its height to only show the first part (with overflow: hidden) 您可以将其高度设置为仅显示第一部分(而不是完全隐藏对象)(溢出:隐藏)
  3. Change the contents of the main object to only have the first part as the contents (requires you to maintain the full contents somewhere else so you can restore it when expanded again). 将主对象的内容更改为仅将第一部分作为内容(要求您将其他内容保留在其他位置,以便在再次展开时可以将其还原)。

Here's a working example of option 1: http://jsfiddle.net/jfriend00/CTzsP/ . 这是选项1的工作示例: http : //jsfiddle.net/jfriend00/CTzsP/

You'd need to either: 您需要:

  • Put in a span/etc. 放入跨度/等。 after the first n words, and only hide that part, or 在前n个单词之后,并且仅隐藏该部分,或者
  • Change the viewable region, or 更改可见区域,或
  • Replace or toggle the span/etc. 替换或切换跨度/等。 with the "collapsed" view. 与“折叠”视图。

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.

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