简体   繁体   English

如何创建类似jsfiddle的所见即所得

[英]How to create a WYSIWYG like jsfiddle

I'm trying to create my own jsfiddle funcionality. 我正在尝试创建自己的jsfiddle函数。

Three textboxes with some codes, but have no idea how to make (and remake) files with their contents. 三个带有某些代码的文本框,但不知道如何使用其内容制作(和重新制作)文件。

Or, is there another way to play the code, without creating files ? 或者,还有另一种无需创建文件即可播放代码的方法吗?

 $('.button').on('click', function(){ // create demo.css, demo.html and demo.js // open a new tab and play all the code }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea class='txthtml'> <div class='parent'> <div class='title'>lorem 01</div> <div class='title'>lorem 02</div> <div class='title'>lorem 03</div> </div> </textarea> <textarea class='txtcss'> .parent{ background:gold; } .title{ margin:5px 0; backgorund:lightgreen; } </textarea> <textarea class='txtjs'> $('.title').on('click', function(){ console.log('clicked'); }); </textarea> <br><br> <button>CLICK</button> 

Below, you can find a very simple example that you want. 在下面,您可以找到所需的非常简单的示例。

For render html into your view, you can juste get the value from your textarea and paste in into your content that when you want to do 为了将html呈现到视图中,您可以从textarea获取value并将其粘贴到内容中,以便在需要时执行

 const code = document.querySelector('.code'); const btn = document.querySelector('.btn'); const render = document.querySelector('.render'); btn.addEventListener('click', () => { const getHTML = code.value; render.innerHTML = getHTML; }) 
 .code { width: 200px; height: 200px; } 
 <textarea class="code"></textarea> <button class="btn">go</button> <div class="render"></div> 

create iframe and write the content to the iframe. 创建iframe并将内容写入iframe。 jsFiddle Demo , not working here because not allowed to aceess iframe. jsFiddle Demo ,在这里不起作用,因为不允许使用ace iframe。

 $('button').on('click', function() { var addJquery = $('#addJqury').is(':checked') var html = $('.txthtml').val(), css = $('.txtcss').val(), js = $('.txtjs').val(), output = '<style>' + css + '</style>' + html + '<script>' + js + '<\\/script>'; if (addJquery) output = '<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"><\\/script>' + output; var iframe = window.results.document; iframe.write(output); iframe.close(); }); 
 textarea{min-height:80px;min-width:200px} iframe{width:100%;height:100%;display:block} 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <textarea class="txthtml"> <div class='parent'> <div class='title'>lorem 01</div> <div class='title'>lorem 02</div> <div class='title'>lorem 03</div> </div> </textarea> <textarea class='txtcss'> .parent{ background:gold; } .title{ margin:5px 0; backgorund:lightgreen; } </textarea> <textarea class='txtjs'> $('.title').on('click', function(){ console.log('clicked: ' + $(this).html()); }); </textarea><br> <input type="checkbox" id="addJqury" checked> Add Jquery? <br><br> <button>CLICK</button> <hr>Results: <hr> <iframe name="results" src="about:blank"></iframe> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM