其他分享
首页 > 其他分享> > 博客园代码块展开与折叠的js

博客园代码块展开与折叠的js

作者:互联网

<script type="text/javascript">
        $(document).ready(function () {
            var pres = $("pre");
            for (var i = 0; i < pres.length; i++) {
                $(pres[i]).attr('id', 'pre' + i);
                $(pres[i]).children('code').hide();
                $(pres[i]).prepend('<button id="btn'+ i +'" onclick="view_code('pre'+ i +'');">view code</button>');
            }
        });
        function view_code (id) {
            var btn_text =  document.getElementById(id).children[0].innerText;
            var style;
            var status;
            if(btn_text == 'view code') {
                style = '""';
                status = '-';
            } else {
                style = 'display: none;';
                status = '+';
            }
            document.getElementById(id).children[0].innerText = status;
            document.getElementById(id).children[1].style = style;
        }
</script>

标签:status,style,折叠,博客园,js,code,var,id,pres
来源: https://www.cnblogs.com/diygou/p/15249728.html