canrun

测试运行HTML
<html>
<head>
    <title>测试博客园HTML源码运行程序</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Language" content="zh-CN" />
    <script type="text/javascript">
        alert('哈哈,我运行咯!!!');
    </script>
</head>
<body>
</body>
</html>

 

参考肥杜的教你如何在博客园放“可运行”代码

自己定制了一个专属博客园的HTML源码运行js文件。

$(document).ready(function(){
    //如果带有canrun标签
    if($("#likecs_post_body p:first").html()&&$("#likecs_post_body p:first").html().toLowerCase().indexOf('canrun') != -1){
        //移除canrun标签
        $("#likecs_post_body p:first").remove();
        //在文章底部加入运行此段代码HTML,可以触发运行html
        var runString = '<input type="button" value="运行此段代码" >;
        if($(".likecs_code").length > 1) runString += '<input type="text" size="5px" >;
        $("#likecs_post_body").append(runString);
        
        //为每一段源码加可运行按钮
        $.each($(".likecs_code"),function(i){
            $(this).before('<input type="button" class="runBtn" onclick="doRunCnblogsHtml('+ i +');" style="cursor:pointer;" value="点此运行此段代码('+ i +')"/>');
        });
    }
    
    //运行此段代码点击触发事件
    $("#runHTML").click(function(){
        var hid = $("#hid").val();        //获取输入的代码段号
        doRunCnblogsHtml(hid);            //弹窗运行相应代码段
    });
    
    //首页,直接移除canrun标签
    var postCons = $(".postCon");
    for(var i=0;i<postCons.length;i++){
        if(postCons.eq(i).children('p:first').html()&&postCons.eq(i).children('p:first').html().toLowerCase().indexOf('canrun') != -1){
            postCons.eq(i).children('p:first').remove();
        }
    }
});
/**
 * 博客园格式化HTML执行函数
 * @params hid 格式化代码的个数index,第一个为0,第二个为1...
 */
function doRunCnblogsHtml(hid){
    if(parseInt(hid) != hid) hid = 0;        //数值型检测
    $(".likecs_code").eq(hid).click();        //点击“+”以查看解析前的HTML源码
    $(".likecs_code").eq(hid).find(".likecs_code_copy").find("a").eq(0).click();        //点击复制按钮以得到解析后的HTML源码
    openWin($(".likecs_code").eq(hid).find('textarea').eq(0).val());        //将解析后的HTML源码在新窗口运行
}
/**
 * 新窗口执行HTML函数
 * @params     content        新窗口内容
 */
function openWin(content){
    var newwin = window.open('', "_blank", '');        //为了简便,这里不设参数
    newwin.document.open('text/html', 'replace');
    newwin.opener = null;
    newwin.document.write(content);        //将内容写入新窗口
    newwin.document.close();
}
ZjmainstayRunHTML.js

相关文章:

  • 2021-12-13
  • 2021-07-11
  • 2022-12-23
  • 2021-12-03
  • 2022-12-23
  • 2021-07-22
  • 2022-12-23
猜你喜欢
  • 2021-06-29
  • 2022-12-23
  • 2021-07-16
  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
  • 2021-11-29
相关资源
相似解决方案