想要創建隨機代碼,將其存盤在變數中并在 html 中顯示。
function GenerateButton() {
document.getElementById("btn1id").style.display = "none";
document.getElementById("Txt").style.display = "block";
document.getElementById("code").innerHTML = GFCode;
}
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result = chars[Math.round(Math.random() * (chars.length - 1))];
return result;
}
function codeFunc() {
document.write(randomString(4, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
document.write("-");
document.write(randomString(4, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
}
var GFCode = codeFunc;
<div class="btn0" id="btn1id" onclick="GenerateButton()">Generate</div>
<div class="Txt" id="Txt" style="display=none;"> The Code is: <span id="code"></span></div>
如果我在生成代碼的 html 檔案中執行 document.write,但我無法從 js 檔案中獲取它。
uj5u.com熱心網友回復:
由于您將 GFCode 變數設定為簡單的函式名稱,因此您將其設定為函式本身,而不是回傳值。要實際運行該函式并獲取它的回傳值,您必須像這樣撰寫它:
var GFCode = codeFunc();
uj5u.com熱心網友回復:
您的問題指出:“...創建隨機代碼,將其存盤在變數中并以 html 顯示。”
不要使用document.write. 只需回傳一個字串,將其分配給一個變數,然后在您的 HTML 中插入該變數。為此,您需要執行codeFunc()然后將回傳的隨機代碼(字串)分配給GFCode:
function GenerateButton(){
document.getElementById("btn1id").style.display = "none";
document.getElementById("Txt").style.display = "block";
document.getElementById("code").innerHTML = GFCode;
}
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) {
result = chars[Math.round(Math.random() * (chars.length - 1))];
}
return result;
}
function codeFunc(){
return ( // <-- return the code string
randomString(4, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
"-"
randomString(4, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
);
}
var GFCode = codeFunc(); // <-- execute codeFunc
<div class="btn0" id="btn1id" onclick="GenerateButton()">Generate</div>
<div class="Txt" id="Txt" style="display=none;"> The Code is: <span id="code"></span></div>
如果,正如您的標題所暗示的那樣,您打算將函式存盤在變數中,而不是函式產生的隨機代碼中,您可以:
...
document.getElementById("code").innerHTML = GFCode(); // <-- execute function
...
var GFCode = codeFunc; // <-- assign function to variable
無論哪種方式,都需要從函式回傳隨機代碼,并且需要執行該函式。
uj5u.com熱心網友回復:
您的代碼回傳 null ,因為您在執行變數之前呼叫了它。
函式 codeFunc() 回傳一個函式。
我在這里更改您的代碼。
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var GFCode = `${randomString(4, chars)} - ${randomString(4, chars)}`;
function GenerateButton(){
document.getElementById("btn1id").style.display = "none";
document.getElementById("Txt").style.display = "block";
document.getElementById("code").innerHTML = GFCode;
}
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result = chars[Math.round(Math.random() * (chars.length - 1))];
return result;
}
.btn0 {
padding: 5px 10px;
border: 1px solid black;
width: 100px;
font-weight: 600;
text-align: center;
cursor: pointer;
}
<div class="btn0" id="btn1id" onclick="GenerateButton()">Generate</div>
<div class="Txt" id="Txt" style="display=none;"> The Code is: <span id="code"></span></div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/489205.html
標籤:javascript html
