[英]Random String Selector based on user input, returned in unparsed HTML format for use in websites
I've been creating HTML buttons that return a result from a list of strings. 我一直在创建HTML按钮,这些按钮从字符串列表中返回结果。 It's rather manual work, and I'm looking for a way to automate it, especially for other people online. 这是相当手工的工作,我正在寻找一种自动化的方法,特别是对于其他在线用户。
Essentially, this is a short script that generates another script, however, since everything is being written and returned in HTML, I'm having some issues preventing it from executing as it's being returned . 从本质上讲,这是一个简短的脚本,可以生成另一个脚本,但是,由于所有内容都是以HTML编写和返回的,因此我遇到了一些问题,无法在返回时执行它 。
<textarea rows="15" cols="60" id="content">
Add your d100 table here
</textarea>
<br>
<button onclick="getDetails()">Codify Me</button>
<br>
<br>
<p id="outputText"></p>
<script>
function getDetails(){
var output = "<button onclick='selectRandom()'>Get result<button><br><p id='outputText'></p><"+"script>function selectRandom(){var randList = [";
var list = document.getElementById("content").value.split('\n');
list.pop();
for (var i = 0; i < list.length; i++) {
output += "\""+list[i]+"\",";
}
output += "][Math.floor(Math.random() * "+list.length-1+")]}<"+"/script>";
document.getElementById("outputText").innerHTML = output;
}
</script>
To clarify, I want my script to produce the following output, as an unparsed string, given a user input of ABCD: 澄清一下,给定用户输入的ABCD,我希望我的脚本将以下输出作为未解析的字符串生成:
"<button onclick='selectRandom()'>Get result<button><br><p id='outputText'></p><script>function selectRandom(){var randList = ["A","B","C","D"][Math.floor(Math.random() * 3)]}</script>"
My current issues are, the tags are interfering with each other, and when output, the code returns a button like this: 我当前的问题是,标签相互干扰,并且在输出时,代码返回如下所示的按钮:
Get result 得到结果
Rather than: 而不是:
<button>Get result<button>
along with the rest of the script, though I will likely add more information as I experiment. 以及脚本的其余部分,尽管我可能会在实验时添加更多信息。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Random</title>
</head>
<body>
<textarea rows="15" cols="60" id="content">
Add your d100 table here
</textarea>
<button onclick="selectRandom()">Codify Me</button>
<p id="outputText"></p>
<script>
function selectRandom() {
var items = document.getElementById('content').value.split('\n');
document.getElementById("outputText").innerHTML = items[Math.floor(Math.random() * items.length)];
}
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.