Jquery 多读/少读

Jquery 多读/少读,jquery,Jquery,我不知道我的代码有什么问题: $(document).ready(function(){ var adjustheight = 80; var moreText = "+ read more"; var lessText = "- less text"; $("div.posted_post .more-block").css('height', adjustheight).css('overflow', 'hidden'); $("div.posted_post").append('[..

我不知道我的代码有什么问题:

$(document).ready(function(){

var adjustheight = 80;
var moreText = "+ read more";
var lessText = "- less text";

$("div.posted_post .more-block").css('height', adjustheight).css('overflow', 'hidden');
$("div.posted_post").append('[...]');

$("a.show").text(moreText);

$(".show").toggle(function()
    {
        $(this).find(".more-block").css('height', 'auto').css('overflow', 'visible');
        $(this).text(lessText);
    }, function(){
        $(this).find(".more-block").css('height', adjustheight).css('overflow', 'hidden');
        $(this).text(moreText);
    });
});
html如下所示:

<div class="posted_post">
    <div class="more-block">
        <p>The Content</p>
            <a class="show"></a>
    </div>
</div>

内容


当我加载页面时,会显示“显示更多”按钮,但一秒钟后它就被隐藏了。这里出了什么问题?

您实际上并没有创建该按钮。你需要做什么

$('<a class="show"/>').text(moreText).appendTo('.posted_post'). 

您尝试使用的切换已在jQuery 1.9中删除(从1.8开始就不推荐使用)

因此,通过调用toggle,您实际上是在切换您的元素(如果您使用的是jq1.9)

顺便说一句:

$(this).find(".more-block")
不会归还任何东西

应该是:

$(this).closest(".more-block")
您可以尝试以下方法:

$(document).ready(function(){

var adjustheight = 80;
var moreText = "+ read more";
var lessText = "- less text";

$("div.posted_post .more-block").css('height', adjustheight).css('overflow', 'hidden');
$("div.posted_post").append('[...]');

$("a.show").text(moreText);

$(".show").click(function()
    {
        if($(this).text()===lessText)
        {
            $(this).closest(".more-block").css('height', adjustheight).css('overflow', 'hidden');
            $(this).text(moreText);
        }
        else
        {
            $(this).closest(".more-block").css('height', 'auto').css('overflow', 'visible');
            $(this).text(lessText);
        }
    });
});

是的,我有锚,但我忘了复制和粘贴它,当我使用每个。。。它使botton为每个帖子切换一次,然后停止。。。奇怪的您的html缺少a.show部分。另外,我对$(this.find(“.more block”)感到非常困惑,这意味着a.show会吞没整个block,但我无法理解您到底想做什么。如果您提供更多的信息(比如body标签数据),更好一些,一些css会有助于添加更多的html。我想这可能是css的问题。谢谢你的提示。。。那么我应该如何更改切换功能呢?现在它在多读和少读文本之间切换。。。但是div desn根本没有扩展:-??@PaulHarbuz,div确实扩展到了这里:(不知道这是否是期望的行为)@PaulHarbuz似乎工作正常(前提是链接在div的开头,否则,ti会隐藏起来)看,是的,你是对的!非常感谢。我会解决这个问题。。我接受了你的回答。再次感谢
$(document).ready(function(){

var adjustheight = 80;
var moreText = "+ read more";
var lessText = "- less text";

$("div.posted_post .more-block").css('height', adjustheight).css('overflow', 'hidden');
$("div.posted_post").append('[...]');

$("a.show").text(moreText);

$(".show").click(function()
    {
        if($(this).text()===lessText)
        {
            $(this).closest(".more-block").css('height', adjustheight).css('overflow', 'hidden');
            $(this).text(moreText);
        }
        else
        {
            $(this).closest(".more-block").css('height', 'auto').css('overflow', 'visible');
            $(this).text(lessText);
        }
    });
});