0

如何使用 html 注释在 div 中拆分一些文本。Lats 说我嗬嗬:

<div id="id1">Some first text <!--more--> here.</div>
<a id=id1>Show/Hide</a>
<div id="id2">Some second text is <!--more--> right here.</div>
<a id=id1>Show/Hide</a>

我不会使用 jquery 或 javascript 来显示或隐藏<!--more-->. 谢谢

4

1 回答 1

2

你可以尝试这样的事情:

给定标记:

<div id="id1">Some first text <!--more--> here.</div>
<a>Hide</a>
<div id="id2">Some second text is <!--more--> right here.</div>
<a>Hide</a>​

添加以下javascript:

//1. On page load, wrap text to be hidden in <span>
$(function() {
    $("div").each(function() {
        var html = $(this).html();
        $(this).html(html.replace('<!--more-->', '<span class="hiddenText">', html) + '</span>');
    });

    // 2. Toggle visibility of span tags when clicking link
    $('a').click(function() {
        if ($(this).html() == 'Hide') {
            $(this).prev('div').children('.hiddenText').hide();
            $(this).html('Show');
        } else {
            $(this).prev('div').children('.hiddenText').show();
            $(this).html('Hide');
        }
    });
});

检查我的jsFiddle以获取工作示例。

更新:更新的解决方案不依赖单独的 CSS 类来隐藏/显示文本。

于 2012-07-12T11:35:37.753 回答