gpt4 book ai didi

javascript - 有没有办法使用js从html文本框中获取文本?

转载 作者:行者123 更新时间:2023-12-04 08:46:59 25 4
gpt4 key购买 nike

现在,我知道这之前已经发布过。在报告之前阅读它。我想尝试制作一个从文本框中获取文本的脚本,并且该文本框具有用户编写的代码。现在我有一个可以在新窗口中打开它的脚本,但是

  const el = document.getElementById('externalLink');

// Create a content for a new page with a script in it
const newPageContent = `
<DOCTYPE html>
<head>
<title>some code</title>
</head>
<body contenteditable>
<h1>Some editable text</h1>
</body>
`;

// Listen on "click" event
el.addEventListener('click', (e) => {
e.preventDefault();

// Create a new window on click
const w = window.open('', '_blank');

// Write your content to it
w.document.write(newPageContent);
});
<!-- Your link !-->
<a id="externalLink" href="#/">Click to run the code</a>
<br>
<input type="text" name="Codebox" value="Code here...">

但是脚本“ var Codebox = document.getElementsByName('txtJob')[0].value”只是作为html脚本运行,因此在结果页面上字面意思是“var Codebox = document.getElementsByName('txtJob')[0].value”。如果有办法绕过这个,或者如果我只是做错了,有人可以帮助我吗?那很好啊! :)

最佳答案

您可以设置 idinput标记并使用 document.getElementById('...').value 获取它的值如下。

const el = document.getElementById('externalLink');

// Create a content for a new page with a script in it
const newPageContent = `
<DOCTYPE html>
<head>
<title>some code</title>
</head>
<body contenteditable>
<h1>Some editable text</h1>
</body>
`;

// Listen on "click" event
el.addEventListener('click', (e) => {
e.preventDefault();

const codeValue = document.getElementById("codeBox").value;
console.log('Code Value', codeValue);

// Create a new window on click
const w = window.open('', '_blank');

// Write your content to it
w.document.write(newPageContent);
});
<!-- Your link !-->
<a id="externalLink" href="#/">Click to run the code</a>
<br>
<input type="text" name="Codebox" id="codeBox" value="Code here...">

关于javascript - 有没有办法使用js从html文本框中获取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64266392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com