我目前是一名菜鸟编码员,所以我需要一些帮助。
我想在页面加载/重新加载时在 html 字段中显示随机文本字符串。我希望它从我拥有的列表中选择一个随机文本字符串。
哦,顺便问一下,我可以有一个 JsFiddle 链接吗(如果可能的话)
~谢谢,卡洛
这应该可以。您可以简单地向保存样本的数组中添加更多句子。
var texts = [
"I am a sentence.",
"Some nice stuff.",
"I am random too!"
];
document.getElementById('randomText').value = texts[Math.floor(Math.random()*texts.length)];
<input type="text" id="randomText">
每次重新加载时都会显示随机数。
// find elements
var banner = $("#banner-message-text")
banner.val(parseInt(Math.random()*18546876546));
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#banner-message {
background: #fff;
border-radius: 4px;
padding: 20px;
font-size: 25px;
text-align: center;
transition: all 0.2s;
margin: 0 auto;
width: 300px;
}
button {
background: #0084ff;
border: none;
border-radius: 5px;
padding: 8px 14px;
font-size: 15px;
color: #fff;
}
#banner-message.alt {
background: #0084ff;
color: #fff;
margin-top: 40px;
width: 200px;
}
#banner-message.alt button {
background: #fff;
color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="banner-message">
<input type="text" id="banner-message-text"/>
</div>
let messages = ["Hi", "Hey", "Hello", "Please make some attempts", "holla"];
// We can use inbuilt Math object to perform mathematical operations.
// to get a random number between 0 and the total size of messages array, we can use it as follows.
// Math.floor will return the largest integer which is less than or equal to that random number.
let message = messages[Math.floor(Math.random()*messages.length)];
// to select your input element and set default value, you can do
$('input').val(message);
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
我不知道,我不会编码,我不知道 我不会写代码