jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 如何在段落末尾添加读取更多链接?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试添加一个链接来阅读段落末尾的更多文本.我希望这个链接在最后的段落中显示如下:

我希望段落附加…和阅读更多链接.

现在我不希望阅读更多链接做任何事情,因为一旦我得到链接,我将添加自己的功能.我只是在努力寻找一种让链接看起来像这样方法.

我一直在看下面的jquery脚本,但是这似乎对字符数有效,并且无论字符限制是什么都会切断最后一个字,然后不显示我想要的方式(链接出现在段落下面) .它还包含一个函数,用于单击链接时发生的事情,由于我缺乏jquery的能力,我一直没有成功编辑.

$(function(){ /* to make sure the script runs after page load */
    $('.customer_experience').each(function(event){ /* SELEct all divs with the  customer_experience class */
        var max_length = 250; /* set the max content length before a read more link will be added */

        if($(this).html().length > max_length){ /* check for content length */
            var short_content   = $(this).html().substr(0,max_length); /* split the content in two parts */
            var long_content    = $(this).html().substr(max_length);

            $(this).html(short_content+
                '<a href="#" class="read_more"><br/>read more...</a>'+
                '<span class="more_text" style="display:none;">'+long_content+'</span>'); /* Alter the html to allow the read more functionality */

            $(this).find('a.read_more').click(function(event){ /* find the a.read_more element within the new html and bind the following code to it */
                event.preventDefault(); /* prevent the a from changing the url */
                $(this).parents('.customer_experience').find('.more_text').show(); /* show the .more_text span */
            });
        }
    });
});

我是否需要使用jQuery才能获得我想要的结果?这可以单独用CSS完成吗?我根本不写jQuery,所以我有点迷茫.

任何有关我可以使用此处的帮助或提示都将非常感激.

编辑添加HTML标记

<div class='comments_container'>
    <img src='images/comment-icon.png'/>
    <h1 class='comments_title'>Customer experience</h1>
    <p class='customer_experience'>$customer_exp_one</p>
</div>

有问题的段落是.customer_experience.

@H_197_21@解决方法
我希望下面的内容会对你有所帮助.

http://jsfiddle.net/X5r8r/1156/

body,input {
    font-family: Calibri,Arial;
    margin: 0px;
    padding: 0px;
}
a {
    color: #0254EB
}
a:visited {
    color: #0254EB
}
#header h2 {
    color: white;
    BACkground-color: #00A1E6;
    margin: 0px;
    padding: 5px;
}
.comment {
    width: 400px;
    BACkground-color: #f0f0f0;
    margin: 10px;
}
a.morelink {
    text-decoration: none;
    outline: none;
}
.morecontent span {
    display: none;
}

大佬总结

以上是大佬教程为你收集整理的jquery – 如何在段落末尾添加读取更多链接?全部内容,希望文章能够帮你解决jquery – 如何在段落末尾添加读取更多链接?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。