javascript - textarea中的HTML代码,弹出结果预览的按钮
问题描述
除了主要部分之外,我已经完成了任务中的所有工作。我不知道如何让我的代码将我的 textarea 内容作为 HTML 代码读取并在新窗口中显示结果。我什至不知道如何在内容出现时显示它们。
我没有保留我访问过的所有不同论坛的完整日志,但这里有几个我正在检查的最后几个。
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Wiki editor
</title>
</head>
<body>
<h1> Rachel </h1>
<script>
var previewOpen;
function preview() {
previewOpen = window.open("", "previewOpen", "width=500, height=600");
previewOpen.document.body.innerHTML = "HTML"; // Get the value of text area and run HTML code
}
function closePreview() {
previewOpen.close();
// function to close the preview
}
</script>
<p>Type your HTML code below:</p>
<textarea rows="20" cols="75">
</textarea>
<!-- textarea for submittance of code to be read and written -->
<br>
<span><input id="preview" type="submit" value="Preview" onClick="preview()">
<input id="closePreview" type="submit" value="Close Preview" onClick="closePreview()"></span> <!-- buttons with event handlers to read areatext and perform functions-->
</body>
</html>
解决方案
下面的代码行将静态“HTML”字符串值分配给新打开的窗口主体。
previewOpen.document.body.innerHTML = "HTML"; // Get the value of text area and run HTML code
如果要获取用户输入的值,则必须获取 的值textarea
。
为此,您可以做这样的事情。
1.首先将id添加到您的textarea
.
<textarea id="userValues" rows="20" cols="75">
2.然后从 中获取用户值function preview()
。
function preview() {
previewOpen = window.open("", "previewOpen", "width=500, height=600");
previewOpen.document.body.innerHTML = document.getElementById("userValues").value; // Get the value of text area and run HTML code
}
干杯!!!
推荐阅读
- arrays - 将单个单元格分配给数组会导致“类型不匹配”
- c# - ComboBox 在 DisplayMember 和 ValueMember 中重复相同的值
- typescript - TypeScript 一种基于接口的可选构造函数参数的更好方法
- laravel - 新用户使用默认注册码注册时 Laravel 运行代码
- ios - 在 SwiftUI 中的 onDelete 执行中添加 func
- jwt - 为什么我无法验证我的 Azaure AD JWT 签名?
- javascript - Cookie 不在页面之间传输 (JS)
- asp.net-mvc - 图片上传功能对我来说无法正常工作
- go - 为什么使用多个以太网连接会降低 I/O 绑定任务的吞吐量
- c# - C# 调用 Rscript 但没有输出