substr_len 与 HTML,显示前 X 个字符和“查看更多”链接(jquery.readmore 插件)

发布于 2024-11-07 21:04:28 字数 1596 浏览 3 评论 0原文

我正在使用一个插件来仅显示文本的前 500 个字符...但它不适用于某些 HTML,我无法重现它,但我认为它可能必须使用 ; 标签,或者

或者可能是
    /

它只是显示整个文本和“查看更多”链接...

有什么想法吗?

这是代码

(function ($) {
$.fn.readmore = function (settings) {

$('.home_excerpt').removeClass('home_excerpt');

var opts =  $.extend({}, $.fn.readmore.defaults, settings);

this.each(function () {
  $(this).data("opts", opts);
  if ($(this).html().length > opts.substr_len) {
    abridge($(this));
    linkage($(this));
  }
});

function linkage(elem) {
  elem.append(elem.data("opts").more_link);
  $(".text_seemore").click( function () {
    $(this).hide();
    $(this).siblings("span:not(.hidden)").hide().siblings("span.hidden").show();
    return false;
  });
}

function abridge(elem) {
  var opts = elem.data("opts");
  var txt = elem.html();
  var len = opts.substr_len;
  var dots = "<span>" + opts.ellipses + "</span>";
  var charAtLen = txt.substr(len, 1);
  while (len < txt.length && !/\s/.test(charAtLen)) {
      len++;
      charAtLen = txt.substr(len, 1);
  }
  var shown = txt.substring(0, len) + dots;
  var hidden = '<span class="hidden" style="display:none;">' + txt.substring(len, txt.length) + '</span>';
  elem.html(shown + hidden);
}

return this;
};

$.fn.readmore.defaults = {
substr_len: 500,
ellipses: '&#8230;',
more_link: '<br /><a href="#" class="text_seemore">See&nbsp;More</a>'
};

 })(jQuery);

I'm using a plugin to show only the first 500 characters of a text... But it's not working with some HTML, I can't reproduce it but I think it could have to be with an <img> tag, or a <blockquote> or maybe an <ul> / <ol>

It just shows the whole text AND the "See More" link...

Any idea?

This is the code

(function ($) {
$.fn.readmore = function (settings) {

$('.home_excerpt').removeClass('home_excerpt');

var opts =  $.extend({}, $.fn.readmore.defaults, settings);

this.each(function () {
  $(this).data("opts", opts);
  if ($(this).html().length > opts.substr_len) {
    abridge($(this));
    linkage($(this));
  }
});

function linkage(elem) {
  elem.append(elem.data("opts").more_link);
  $(".text_seemore").click( function () {
    $(this).hide();
    $(this).siblings("span:not(.hidden)").hide().siblings("span.hidden").show();
    return false;
  });
}

function abridge(elem) {
  var opts = elem.data("opts");
  var txt = elem.html();
  var len = opts.substr_len;
  var dots = "<span>" + opts.ellipses + "</span>";
  var charAtLen = txt.substr(len, 1);
  while (len < txt.length && !/\s/.test(charAtLen)) {
      len++;
      charAtLen = txt.substr(len, 1);
  }
  var shown = txt.substring(0, len) + dots;
  var hidden = '<span class="hidden" style="display:none;">' + txt.substring(len, txt.length) + '</span>';
  elem.html(shown + hidden);
}

return this;
};

$.fn.readmore.defaults = {
substr_len: 500,
ellipses: '…',
more_link: '<br /><a href="#" class="text_seemore">See More</a>'
};

 })(jQuery);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-11-14 21:04:28

如果我正确地阅读了您的代码,那么在您的 abridge() 函数中,您似乎试图避免将单词切成两半。问题是,如果所需长度之后没有任何空格,您将只返回整个内容,但您不会事后检查是否是这种情况。这意味着您将返回全文并添加更多链接。

当您处理 HTML 时,如果您有单字行、具有单字单元格的表格等,或者使用   作为空格,则上述情况是常见情况。

更大的问题是您将 HTML 剪切为字符串,这几乎肯定会导致标记损坏。您可以轻松地将标签切成两半,剥离块元素的结束标签,等等。看看是否可以使用 elem.text() 将其处理为纯字符串,或者如果您想保留格式,请查看是否可以提取第一段或类似的内容。

您当然可以实现一些检查和关闭剥离标签的高级方法,但这可能不值得麻烦。我会检查是否有一个插件已经可以满足您的需要。

It seems that in your abridge() function you're trying to avoid cutting a word in half, if I'm reading your code right. The problem is that if there isn't any whitespace after the desired length, you will just return the whole thing, but you're not checking afterwards if that's the case. That means you will return the full text AND put the more link in too.

As you're processing HTML, the above is a usual case if you're having single word lines, tables with single word cells, etc., of if you're using   as whitespace.

The bigger problem is that you're cutting the HTML as string, which will almost certainly result in broken markup. You can easily cut tags in half, strip block elements' closing tags, and so on. See if you can use elem.text() to process it as pure string, or if you want to keep the formatting, see if you can extract the first paragraph, or something similar.

You can of course implement some advanced method of checking and closing stripped tags, but it might not worth the trouble. I'd check if there's a plugin out there which already does what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文