[原] 用 javascript 給你的部落格增加運行代碼功能

來源:互聯網
上載者:User

看到別人部落格有運行代碼的功能不時的羨慕不已呀。想想自己經常發javascript 沒有這東西怎麼行,可惜我的部落格又沒內建這個功能,只好自己弄弄。代碼比較簡單,大家看看笑笑。

使用方法:
<textarea class="runCode">要啟動並執行代碼</textarea>

這裡看看效果 http://dubox.cn/content-26.aspx

 function page_Init(){
  var obj=document.getElementsByTagName("textarea");
    for(i=0;i<obj.length;i++){
      if(obj[i].className=="runCode"){
         var _temid="code_"+i;
         
        
         var html="<textArea style=\"width:80%;height:200px;border-width:1px;border-style:inset\" id=\""+_temid+"\">";
             html+=obj[i].value;
             html+="</textArea><br>";
             html+="<input type=\"button\" value=\"運行代碼\" onClick=\"run('"+_temid+"')\" style=\"border-width:1px\"/><em>(提示:您可以修改代碼後運行)</em>";
         var newobj=document.createElement("div");
             newobj.style.border="1px solid #c0c0c0";
             newobj.style.backgroundColor="#f5f7f7";
             newobj.style.padding="10px";
             newobj.innerHTML=html;
             
             
              if(document.all){
             obj[i].replaceNode(newobj);
             }else{
             obj[i].parentNode.replaceChild(newobj,obj[i]); 
             }

             
      }
    }
 }
 function run(string_id){
 var _code=document.getElementById(string_id).value;
 var win = window.open('', "_blank", '');
        win.document.open('text/html', 'replace');
        win.document.writeln(_code);
 }

window.onload=page_Init;

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.