博客園設置裏申請js權限。做用到側邊欄就能夠,其餘地方也行。只適配的markdown編輯器,與其餘界面美化代碼一塊兒使用可能會出現bug。javascript
<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 = 'hide code'; } else { style = 'display: none;'; status = 'view code'; } document.getElementById(id).children[0].innerText = status; document.getElementById(id).children[1].style = style; } </script>