Javascript 这样可以创建iframe吗?
我想创建这样的iframe。但我没把它弄脏Javascript 这样可以创建iframe吗?,javascript,php,iframe,Javascript,Php,Iframe,我想创建这样的iframe。但我没把它弄脏 <iframe> <html> <style>/* codes */</style> <script type="javascript">/* codes */</script> </html> <body> Welcome!! </body> </iframe&g
<iframe>
<html>
<style>/* codes */</style>
<script type="javascript">/* codes */</script>
</html>
<body>
Welcome!!
</body>
</iframe>
PHP代码:
function prew($html, $css, $js) {
$iframe .= '<iframe>';
$iframe .= '<html>';
$iframe .= '<head>';
$iframe .= '<style>' . $css . '</style>';
$iframe .= '<script>' . $js . '</script>';
$iframe .= '<script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>';
$iframe .= '</head>';
$iframe .= '<body>';
$iframe .= $html;
$iframe .= '</body>';
$iframe .= '</html>';
$iframe .= '</iframe>';
return $iframe;
}
如果我用这个,它就不起作用了。为什么它不起作用?
如何创建所需的iframe?我已经搜索了很多次,但是我找不到关于这种情况的任何信息你不能用PHP直接回显一个包含所有内容的。它们必须与javascript并行工作。在本例中,我将使用jQuery
你的电话在哪里?简单地定义不工作。还有一个目的。。只需在DIV中显示您的内容。我看不到正在调用prev函数…我正在执行联机编辑器。我应该使用iframe@A1rPuni,我认为这是相关的
<?php
// just the same as your code; construction of tags from PHP
function prew($html, $css, $js) {
$iframe = '<html>';
$iframe .= '<head>';
$iframe .= '<style>' . $css . '</style>';
$iframe .= '<script>' . $js . '</script>';
$iframe .= '<script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>';
$iframe .= '</head>';
$iframe .= '<body>';
$iframe .= $html;
$iframe .= '</body>';
$iframe .= '</html>';
return $iframe;
}
// you client should request this
if(isset($_POST['get_contents'])) {
// this is what you decide what you need to put inside, this is just a sample.
echo prew('<div>hello world</div>', 'div{background-color: blue; width: 200px; color: white}', '');
exit;
}
?>
<div class="contents"></div>
<button id="put_contents">Put contents</button>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#put_contents').on('click', function(){
$.ajax({
url: 'index.php', // the name of the php (in this case, same page)
data: {get_contents: true},
type: 'POST',
dataType: 'text',
success: function(response) {
// put the contents
var $iframe = $('.contents').html('<iframe></iframe');
$iframe.html(response);
}
});
});
});
</script>