简体   繁体   English

如何编写HTML内容以使用JQuery打开新窗口

[英]how to write html Content To open New Window with JQuery

on click run button open new window with data of html, css and javascript code editor. 点击运行按钮打开带有html,css和javascript代码编辑器数据的新窗口。 css is pass in new window but html data not, in html using code mirror that can pass in javascipt css在新窗口中传递但html数据未传递,在html中使用可以在javascipt中传递的代码镜像

run botton code is here 运行botton代码在这里

<div style="float:left; width:100%; height:93px;">
    <span class="containerTitle" style="top:65px;">
        <a href="javascript:;" class="button_example" id="result" style="color: white;">Result</a>
    </span>
</div>

on this button call function "nwin" 在此按钮上调用函数“ nwin”

$("a#result").click(nWin);

nwin function is, nwin函数是

<script>
    function nWin() {
        var w = window.open();
        $(w.document.body).html("<style>" + $('#css').val() + "</style>"+ $('#html').val() );
    }
</script>

html data codemirror is, html数据代码镜像是,

<div class="codecontainer" id="htmlContainer" style="max-width:40%;">
    <span class="containerTitle">HTML</span>
    <textarea class="code" id="html" style="display: none;"></textarea>     
</div>

that pass value in editor variable, 在编辑器变量中传递值,

<script>
    var editor = CodeMirror.fromTextArea(document.getElementById("html"), {
        lineNumbers: true,
        mode: "text/html",
        matchBrackets: true
    });
</script>

how to set the value editor in function nwin in html window 如何在HTML窗口的函数nwin中设置值编辑器

I think you need to save from the code editor to the text area before getting the value. 我认为您需要先从代码编辑器save到文本区域,然后再获取值。 It is working in my example 在我的例子中

http://jsfiddle.net/em1uz745/2/ http://jsfiddle.net/em1uz745/2/

Put the same code in a snippet for reference, but it appears that the window.open is blocked: 将相同的代码放在代码段中以供参考,但似乎window.open被阻止了:

 $("a#result").click(nWin); var cssEditor = CodeMirror.fromTextArea(document.getElementById("css"), { lineNumbers: true, mode: "text/css", matchBrackets: true }); var htmlEditor = CodeMirror.fromTextArea(document.getElementById("html"), { lineNumbers: true, mode: "text/html", matchBrackets: true }); function nWin() { // Commit the code to the textarea so that it can be extracted using value cssEditor.save(); htmlEditor.save(); var w = window.open(); $(w.document.head).append("<style>" + $('#css').val() + "</style>"); $(w.document.body).html($('#html').val() ); } 
 body { font-family: sans-serif; font-size: 14px; } .button_example { display: inline-block; background: #ccc; border-radius: 3px; border: 1px solid #333; color: #333; padding: 10px 20px; text-decoration: none; } .button_example:hover { background: #aaa; } .codecontainer { width: 50%; float: left; } .clearfix { clear: both; } } 
 <link href="http://codemirror.net/lib/codemirror.css" rel="stylesheet"/> <script src="http://codemirror.net/lib/codemirror.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="button_wrap"> <span class="containerTitle"> <a href="javascript:;" class="button_example" id="result">Result</a> </span> </div> <div class="codecontainer" id="cssContainer"> <span class="containerTitle">CSS</span> <textarea class="code" id="css"></textarea> </div> <div class="codecontainer" id="htmlContainer"> <span class="containerTitle">HTML</span> <textarea class="code" id="html"></textarea> </div> <div class="clearfix"></div> 

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

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