How to use JS to directly run html code

不言
Release: 2018-06-09 16:53:37
Original
5105 people have browsed it

This article mainly introduces the method of JS to directly run html code, involving the implementation skills related to javascript window operation. Friends in need can refer to it

The example of this article describes the method of JS to directly run html code. . Share it with everyone for your reference, the details are as follows:

1. Example code:

<!DOCTYPE html>
<html>
  <head>
    <meta charset=&#39;utf-8&#39;/>
    <title>直接运行 html 代码</title>
  </head>
  <body>
    <textarea style=&#39;width:300px;height:200px;&#39; id=&#39;txtCode&#39;></textarea><br/>
    <input type=&#39;button&#39; value=&#39;直接运行&#39; id=&#39;btnRun&#39;/>
    <script>
      document.getElementById(&#39;btnRun&#39;).onclick = function(){
        var runHtml = document.getElementById(&#39;txtCode&#39;).value;
        if(runHtml){
          var win = window.open(&#39;&#39;, &#39;运行窗口&#39;);
          win.document.open();
          win.document.write(runHtml);
          win.document.close();
        }
        else{
          alert(&#39;请输入!&#39;);
        }
      }
    </script>
  </body>
</html>
Copy after login

2. The running effect diagram is as follows:

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Using Requirejs in Html for modular development analysis

HTML5 and jQuery realize search intelligent matching function

The above is the detailed content of How to use JS to directly run html code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!