使用read more链接创建摘录文本

时间:2009-10-07 00:00:59

标签: jquery asp-classic

我有一个充满文本的varchar字段,我希望能够只显示100个字符的文本片段,并在片段末尾显示“阅读更多...”链接。当用户点击“阅读更多...”时,我希望页面展开并显示文本的其余部分。

我想'show / hide'功能可以用jQuery完成,但我不确定ASP是否有一些功能可以有效地将文本的varchar字段分成两部分?

我的内容目前正在使用;

进入页面
<%=StripHTML(rspropertyresults.Fields.Item("ContentDetails").Value)%>

使用此功能去除任何HTML标记;

<%
Function stripHTML(strHTML)
  ''Strips the HTML tags from strHTML

  Dim objRegExp, strOutput
  Set objRegExp = New Regexp

  objRegExp.IgnoreCase = True
  objRegExp.Global = True
  objRegExp.Pattern = "<(.|\n)+?>"

  ''Replace all HTML tag matches with the empty string
  strOutput = objRegExp.Replace(strHTML, "")

  ''Replace all < and > with &lt; and &gt;
  strOutput = Replace(strOutput, "<", "&lt;")
  strOutput = Replace(strOutput, ">", "&gt;")

  stripHTML = strOutput    ''Return the value of strOutput

  Set objRegExp = Nothing
End Function
%>

3 个答案:

答案 0 :(得分:12)

尝试以此为出发点。

<p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas massa lectus, pulvinar vel scelerisque eget, 
    rutrum et nisi. Mauris semper viverra lorem sit amet faucibus. Fusce egestas metus sit amet lectus interdum 
    sollicitudin. Maecenas accumsan metus scelerisque tortor lobortis et pretium nibh cursus.
</p>
<script type="text/javascript">
    $(function() {
        var textToHide = $('p').text().substring(100);
        var visibleText = $('p').text().substring(1, 100);

        $('p')
            .html(visibleText + ('<span>' + textToHide + '</span>'))
            .append('<a id="read-more" title="Read More" style="display: block; cursor: pointer;">Read More&hellip;</a>')
            .click(function() {
                $(this).find('span').toggle();
                $(this).find('a:last').hide();
            });

        $('p span').hide();
    });
</script>

所以我在这里做的是创建两个变量:一个用于保存前100个字符(“visibleText”),另一个用于保存其余的(“textToHide”)。

然后我们告诉jQuery找到每个段落标记(你可能想要定义一个更具体的选择器),将文本包装在span标签中并将其全部放回可见文本上,在末尾附加一个链接这一切都是为了显示我们将要隐藏的文本,最后分配一个点击事件来完成它。

点击功能只是查找段落中的所有span标签,将它们切换为可见(实际上show()功能可能是更好的选择),然后隐藏“阅读更多”链接。

最后,我们确保段落的span标签开始被隐藏。您可能实际上想要创建一个CSS规则(p span {display: none;}),以便文本仍然隐藏起来,但比JavaScript更快。 jQuery show()函数在调用时仍将覆盖该css规则。

应该这样做。

答案 1 :(得分:1)

扩展Phil Wheeler的示例,下面是一个示例,它使用jQuery的Each函数将其应用于表中的每个td元素。

<html>
<head>
    <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.4.min.js" 
            type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("td").each(
                function( intIndex ) {
                    var textToHide = $(this).text().substring(100);
                    var visibleText = $(this).text().substring(1, 100);

                    $(this)
                        .html(visibleText + ('<span>' + textToHide + '</span>'))
                        .append('<a id="read-more" title="Read More" style="display: block; cursor: pointer;">Read More&hellip;</a>')
                        .click(function() {
                            $(this).find('span').toggle();
                            $(this).find('a:last').toggle();
                        });

                    $(this).find("span").hide();
                }
            )

        });
    </script>
</head>
<body>
    <table border="1">
        <tr>
            <td>
                "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."
            </td>
            <td>
                "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
            </td>
        </tr>
    </table>
</body>

答案 2 :(得分:0)

一旦你了解它,你所追求的一切都是可行的,而且非常容易。我不是一个jquery人(Mootools对我来说!)所以无法帮助你。

对于ASP截断功能,你需要小心。你有正确的想法,虽然剥离html,否则你可以打开一个未关闭的标签(一个未封闭的div或脚本标签可能真的搞乱你的布局)。

我会:

  • 首先剥开它
  • 如果长度小于您想要通过显示它来截断它
  • 其他明智地使用Left(your_string,300)只捕获前300个字符(例如)然后在末尾附加“...”或者什么。好,易于。

你可以试着想象一下并尝试将它拆分到下一个空间,这样你就不会将单词切成两半而不记得我在VBScript中如何做到这一点。 JS没问题!