前端jQuery 文字超出省略号,点击显示全部、收起

前端开发的命名规范
web前端开发规范 实现符合w3c的web标准
公司前端开发规范

html 代码

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
        <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
        <title>jquery 文字超出省略号,点击显示全部、收起</title>
        <script src=”http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js”></script>
    </head>
    <body>
        <div class=”desc”>
            <ul>
                <li>
                    <div class=”branddesc”>
                        两句话深度开发几号上的佛牌维护佛文化的开户费收到了家很富是的开户费了色块大黄蜂评委会分是开将恢复时来得快积分还圣诞快乐抵抗力金凤凰收到了开间房
                    </div>
                </li>
                <li>
                    <div class=”branddesc”>
                        复合弓放假光华科技个我快让他也未发生的分数线从V型从v错把女女过一会就返回二过水电费三的丰盛的v格式规范换个环境感觉感觉吃吧许不舒服个的非国有etymology返回返回
                    </div>
                </li>
            </ul>
        </div>
        <script>
            ;(function($) {
                $.fn.moreText = function(options) {
                    var defaults = {
                        maxLength: 50,
                        mainCell: ‘.branddesc’,
                        openBtn: ‘显示全部’,
                        closeBtn: ‘收起’,
                    }
                    return this.each(function() {
                        var _this = $(this)
                        var opts = $.extend({}, defaults, options)
                        var maxLength = opts.maxLength
                        var TextBox = $(opts.mainCell, _this)
                        var openBtn = opts.openBtn
                        var closeBtn = opts.closeBtn
                        var countText = TextBox.html()
                        var newHtml = ”
                        if (countText.length > maxLength) {
                            newHtml =
                                countText.substring(0, maxLength) +
                                ‘…<span class=”more”>’ +
                                openBtn +
                                ‘</span>’
                        } else {
                            newHtml = countText
                        }
                        TextBox.html(newHtml)
                        TextBox.on(‘click’, ‘.more’, function() {
                            if ($(this).text() == openBtn) {
                                TextBox.html(
                                    countText +
                                        ‘ <span class=”more”>’ +
                                        closeBtn +
                                        ‘</span>’,
                                )
                            } else {
                                TextBox.html(newHtml)
                            }
                        })
                    })
                }
            })(jQuery)
            $(function() {
                $(‘.desc ul li’).moreText({
                    maxLength: 10, //默认最大显示字数,超过…
                    mainCell: ‘.branddesc’, //文字容器
                })
            })
        </script>
    </body>
</html>

javascript 代码

;(function($) {
    $.fn.moreText = function(options) {
        var defaults = {
            maxLength: 50,
            mainCell: ‘.branddesc’,
            openBtn: ‘显示全部’,
            closeBtn: ‘收起’,
        }
        return this.each(function() {
            var _this = $(this)
            var opts = $.extend({}, defaults, options)
            var maxLength = opts.maxLength
            var TextBox = $(opts.mainCell, _this)
            var openBtn = opts.openBtn
            var closeBtn = opts.closeBtn
            var countText = TextBox.html()
            var newHtml = ”
            if (countText.length > maxLength) {
                newHtml =
                    countText.substring(0, maxLength) +
                    ‘…<span class=”more”>’ +
                    openBtn +
                    ‘</span>’
            } else {
                newHtml = countText
            }
            TextBox.html(newHtml)
            TextBox.on(‘click’, ‘.more’, function() {
                if ($(this).text() == openBtn) {
                    TextBox.html(
                        countText +
                            ‘ <span class=”more”>’ +
                            closeBtn +
                            ‘</span>’,
                    )
                } else {
                    TextBox.html(newHtml)
                }
            })
        })
    }
})(jQuery)
$(function() {
    //调用
    $(‘.desc ul li’).moreText({
        maxLength: 10, //默认最大显示字数,超过…
        mainCell: ‘.branddesc’, //文字容器
    })
})
web前端开发命名规范
前端开发规范 意义
前端开发文件名称规范
» 本文来自:前端开发者 » 《前端jQuery 文字超出省略号,点击显示全部、收起》
» 本文链接地址:https://www.rokub.com/6315.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!