gpt4 book ai didi

jquery - 如何用dotdotdot实现多读和少读?

转载 作者:行者123 更新时间:2023-12-03 22:20:28 28 4
gpt4 key购买 nike

使用 jQuery dotdotdot插件,我想要有一个更多更少按钮来显示和隐藏 <div> 的全部内容当有大量文本需要显示时。 更多按钮工作得很好,但我还没有找到返回 <div> 的方法。到原来的显示。请注意,这不仅仅是关于如何使用 dotdotdot 来扩展截断的字符串,因为它包含了重新截断长字符串的 Less 按钮。

这里是my code :

$(function() {
$("div.ellipsis-text").dotdotdot({
after: 'a.more',
callback: dotdotdotCallback
});
$("div.ellipsis-text").find("a").click(function() {
if ($(this).text() == "More") {
var div = $(this).closest('div.ellipsis-text');
div.trigger('destroy').find('a.more').hide();
div.css('max-height', '');
$("a.less", div).show();
}
else {
$(this).text("More");
$(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({ after: "a", callback: dotdotdotCallback });
}
});

function dotdotdotCallback(isTruncated, originalContent) {
if (!isTruncated) {
$("a", this).remove();
}
}
});

看来click <div> 的事件处理程序的 anchor 标记被删除,单击更多按钮后我永远无法到达事件处理程序。

找到解决方案:

Updated code :

$(function() {
$("div.ellipsis-text").dotdotdot({
after: 'a.more',
callback: dotdotdotCallback
});
$("div.ellipsis-text").on('click','a',function() {
if ($(this).text() == "More") {
var div = $(this).closest('div.ellipsis-text');
div.trigger('destroy').find('a.more').hide();
div.css('max-height', '');
$("a.less", div).show();
}
else {
$(this).hide();
$(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({ after: "a.more", callback: dotdotdotCallback });
}
});

function dotdotdotCallback(isTruncated, originalContent) {
if (!isTruncated) {
$("a", this).remove();
}
}
});

谢谢大家!

最佳答案

更改此:

$("div.ellipsis-text").find("a").click(function() {

对此:

$("div.ellipsis-text").on('click','a',function() {

更新的代码:

$(function () {
$("div.ellipsis-text").dotdotdot({
after: 'a.more',
callback: dotdotdotCallback
});
$("div.ellipsis-text").on('click', 'a', function () {

if ($(this).text() == "More") {
var div = $(this).closest('div.ellipsis-text');
div.trigger('destroy').find('a.more').hide();
div.css('max-height', '');
$("a.less", div).show();
} else {

$(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({
after: "a",
callback: dotdotdotCallback
});
}
});

function dotdotdotCallback(isTruncated, originalContent) {
if (!isTruncated) {
$("a", this).remove();
}
}
});

第二种方法是使用单个 a 标签并以这种方式切换其文本:

HTML:

    <div class='ellipsis-text'>"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?" 

<a class='more' href='#'>More</a>

</div>

JQUERY:

$(function () {
$("div.ellipsis-text").dotdotdot({
after: 'a.more',
callback: dotdotdotCallback
});
$("div.ellipsis-text").on('click', 'a', function () {

if ($(this).text() == "More") {
var div = $(this).closest('div.ellipsis-text');
div.trigger('destroy')
div.css('max-height', '');
$(this).text("Less");

} else {
$(this).text("More")
$(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({
after: "a",
callback: dotdotdotCallback
});
}
});

function dotdotdotCallback(isTruncated, originalContent) {
if (!isTruncated) {
$("a", this).remove();
}
}
});

关于jquery - 如何用dotdotdot实现多读和少读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25187774/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com