由于某種原因,沒有定義元素Id,所有代碼看起來都很好。
function qm() {
var imgsiz = Math.floor(Math.random() * (100 - 1)) + 1
console.log(imgsiz)
if (imgsiz=="1"){
document.getElementById("image").innerHTML = "<img src=1.jpg>"
} else if (imgsiz=="2"){
document.getElementById("image").innerHTML = "<img src=2.jpeg>"
} else if (imgsiz=="3"){
document.getElementById("image").innerHTML = "<img src=3.jpg>"
} else if (imgsiz=="4"){
document.getElementById("image").innerHTML = "<img src=4.jpg>"
} else if (imgsiz=="5"){
document.getElementById("image").innerHTML = "<img src=5.jpg>"
} else if (imgsiz=="6"){
document.getElementById("image").innerHTML = "<img src=6.jpeg>"
} else if (imgsiz=="7"){
document.getElementById("image").innerHTML = "<img src=7.jpg>"
} else if (imgsiz=="8"){
document.getElementById("image").innerHTML = "<img src=8.jpg>"
} else if (imgsiz=="9"){
document.getElementById("image").innerHTML = "<img src=9.jpg>"
} else if (imgsiz=="10"){
document.getElementById("image").innerHTML = "<img src=10.jpg>"
} else if (imgsiz=="11"){
document.getElementById("image").innerHTML = "<img src=11.jpeg>"
} else if (imgsiz=="12"){
document.getElementById("image").innerHTML = "<img src=12.jpg>"
} else if (imgsiz=="13"){
document.getElementById("image").innerHTML = "<img src=13.jpg>"
} else if (imgsiz=="14"){
document.getElementById("image").innerHTML = "<img src=14.jpeg>"
} else if (imgsiz=="15"){
document.getElementById("image").innerHTML = "<img src=15.jpeg>"
} else if (imgsiz=="16"){
document.getElementById("image").innerHTML = "<img src=16.jpeg>"
} else if (imgsiz=="17"){
document.getElementById("image").innerHTML = "<img src=1.jpg>"
}
var fonts = [ '1','2','1','2','3','4','5','6','5','6','7','8'];
var font = fonts[Math.floor(Math.random()*fonts.length)];
if (font=="1"){
document.getElementById("quote")
quote.style.fontFamily = "arial"
} else if (font=="2"){
document.getElementById("quote")
quote.style.fontFamily = "tahoma"
} else if (font=="3"){
document.getElementById("quote")
quote.style.fontFamily = "trebuchet"
} else if (font=="4"){
document.getElementById("quote")
quote.style.fontFamily = "times new roman"
} else if (font=="5"){
document.getElementById("quote")
quote.style.fontFamily = "georgia"
} else if (font=="6"){
document.getElementById("quote")
quote.style.fontFamily = "garamond"
} else if (font=="7"){
document.getElementById("quote")
quote.style.fontFamily = "courier new"
} else if (font=="8"){
document.getElementById("quote")
quote.style.fontFamily = "brush script mt"
}
var rndoInt = Math.floor(Math.random() * (25 - 15)) + 15
console.log(rndoInt)
document.getElementById("quote");
quote.style.fontSize = rndoInt + "px";
var nouns = [' time ' , ' year ' , ' device ' , ' way ' , ' day ' , ' man ' , ' thing ' , ' woman ' , ' life ' , ' child ' , ' world ' , ' school ' , ' state ' , ' family ' , ' student ' , ' group ' , ' country ' , ' problem ' , ' hand ' , ' part ' , ' place ' , ' self ' , ' week ' , ' company ' , ' system ' , ' program ' , ' question ' , ' food ' , ' government ' , ' number ' , ' night ' , ' point ' , ' home ' , ' water ' , ' room ' , ' mother ' , ' area ' , ' money ' , ' story ' , ' bone ' , ' month ' , ' furniture ' , ' right ' , ' study ' , ' book ' , ' eye ' , ' job ' , ' word ' , ' business ' , ' issue ' , ' side ' , ' kind ' , ' head ' , ' house ' , ' service ' , ' friend ' , ' father ' , ' power ' , ' hour ' , ' game ' , ' line ' , ' clothing ' , ' member ' , ' law ' , ' car ' , ' city ' , ' community ' , ' name ' , ' president ' , ' team ' , ' minute ' , ' idea ' , ' kid ' , ' body ' , ' information ' , ' back ' , ' parent ' , ' face ' , ' innocent ' , ' level ' , ' office ' , ' door ' , ' health ' , ' person ' , ' life ' , ' war ' , ' history ' , ' party ' , ' dog ' , ' change ' , ' morning ' , ' partner ' , ' funeral ' , ' girl ' , ' guy ' , ' moment ' , ' air ' , ' teacher ' , ' force ' , ' educator' ];
var noun = nouns[Math.floor(Math.random()*nouns.length)];
var pnouns = [' times ' , ' years ' , ' devices ' , ' ways ' , ' days ' , ' men ' , ' things ' , ' women ' , ' lives ' , ' children ' , ' worlds ' , ' schools ' , ' states ' , ' families ' , ' students ' , ' groups ' , ' countries ' , ' problems ' , ' hands ' , ' parts ' , ' places ' , ' selves ' , ' weeks ' , ' companies ' , ' systems ' , ' programs ' , ' questions ' , ' food ' , ' governments ' , ' numbers ' , ' nights ' , ' points ' , ' homes ' , ' waters ' , ' rooms ' , ' mothers ' , ' areas ' , ' money ' , ' stories ' , ' bones ' , ' months ' , ' furniture ' , ' rights ' , ' studies ' , ' books ' , ' eyes ' , ' jobs ' , ' words ' , ' business ' , ' issues ' , ' sides ' , ' people ' , ' heads ' , ' houses ' , ' services ' , ' friends ' , ' fathers ' , ' powers ' , ' hours ' , ' games ' , ' lines ' , ' clothing ' , ' members ' , ' laws ' , ' cars ' , ' cities ' , ' communities ' , ' names ' , ' presidents ' , ' teams ' , ' minutes ' , ' ideas ' , ' kids ' , ' bodies ' , ' information ' , ' backs ' , ' parents ' , ' faces ' , ' innocents ' , ' levels ' , ' offices ' , ' doors ' , ' health ' , ' people ' , ' lives ' , ' wars ' , ' histories ' , ' parties ' , ' dogs ' , ' changes ' , ' mornings ' , ' partners ' , ' funerals ' , ' girls ' , ' guys ' , ' moments ' , ' airs ' , ' teachers ' , ' forces ' , ' educators ' ];
var pnoun = pnouns[Math.floor(Math.random()*pnouns.length)];
var adverbs = [' bashfully ' , ' beautifully ' , ' bitterly ' , ' bleakly ' , ' blindly ' , ' blissfully ' , ' boastfully ' , ' boldly ' , ' bravely ' , ' briefly ' , ' brightly ' , ' briskly ' , ' broadly ' , ' busily ' , ' calmly ' , ' carefully ' , ' carelessly ' , ' cautiously ' , ' certainly ' , ' cheerfully ' , ' clearly ' , ' cleverly ' , ' closely ' , ' coaxingly ' , ' colorfully ' , ' commonly ' , ' continually ' , ' coolly ' , ' correctly ' , ' courageously ' , ' crossly ' , ' cruelly ' , ' curiously ' , ' daily ' , ' daintily ' , ' dearly ' , ' deceivingly ' , ' deeply ' , ' defiantly ' , ' deliberately ' , ' delightfully ' , ' diligently ' , ' dimly ' , ' doubtfully ' , ' dreamily ' , ' easily ' , ' elegantly ' , ' energetically ' , ' enormously ' , ' enthusiastically ' , ' equally ' , ' especially ' , ' evenly ' , ' fortunately ' , ' frankly ' , ' frantically ' , ' freely ' , ' frenetically ' , ' hopelessly ' , ' shrilly ' , ' shyly ' , ' silently ' , ' sleepily ' , ' slowly ' , ' smoothly ' , ' publicly ' , ' solemnly ' , ' solidly ' , ' sometimes ' , ' rapidly ' , ' speedily ' , ' stealthily ' , ' sternly ' , ' strictly ' , ' unaccountably ' , ' unbearably ' , ' unethically ' , ' unexpectedly ' , ' unfortunately ' , ' unimpressively ' , ' unnaturally ' , ' unnecessarily ' , ' angrily ' , ' equally ' , ' blissfully ' , ' purposefully ' , ' vainly ' , ' valiantly ' , ' vastly ' , ' verbally ' , ' upsettingly ' , ' viciously ' , ' victoriously ' , ' violently ' , ' weakly ' , ' wearily ' , ' deppresingly ' , ' wetly ' , ' fully ' , ' wildly '];
var adverb = adverbs[Math.floor(Math.random()*adverbs.length)];
var verbs = [ ' be ' , ' keep ' , ' understand ' , ' shoot ' , ' beat ' , ' watch ' , ' do ' , ' begin ' , ' follow ' , ' say ' , ' devour ' , ' stop ' , ' abolish ' , ' help ' , ' create ' , ' suck ' , ' drown ' , ' speak ' , ' get ' , ' fight ' , ' read ' , ' choke ' , ' start ' , ' escape ' , ' make ' , ' trust ' , ' add ' , ' know ' , ' show ' , ' spend ' , ' steal ' , ' hear ' , ' grow ' , ' think ' , ' misuse ' , ' open ' , ' take ' , ' run ' , ' release ' , ' see ' , ' move ' , ' win ' , ' punch ' , ' like ' , ' offer ' , ' obliterate ' , ' live ' , ' remember ' , ' want ' , ' believe ' , ' love ' , ' steal ' , ' hold ' , ' consider ' , ' use ' , ' bring ' , ' find ' , ' break ' , ' accept ' , ' buy ' , ' give ' , ' rejoin ' , ' attend ' , ' tell ' , ' write ' , ' serve ' , ' work ' , ' provide ' , ' detonate ' , ' end ' , ' trust ' , ' deport ' , ' start ' , ' burn ' , ' expect ' , ' call ' , ' lose ' , ' build ' , ' try ' , ' pay ' , ' like ' , ' rob ' , ' meet ' , ' fail ' , ' need ' , ' include ' , ' cut ' , ' feel ' , ' sell ' , ' reach ' , ' become ' , ' blow ' , ' kill ' , ' leave ' , ' learn ' , ' ruin ' , ' bury ' , ' change ' , ' bully ' , ' lead '];
var verb = verbs[Math.floor(Math.random()*verbs.length)];
var fronttexts = ['Always ','Never ','You only live once, ','Life is short ','Trust people that ','Dont trust people who ','Occasionally ','If you ever feel sad, ','If you are feeling up to it, ',''];
var fronttext = fronttexts[Math.floor(Math.random()*fronttexts.length)];
var adjectives = [' other ' , ' new ' , ' good ' , ' high ' , ' old ' , ' great ' , ' big ' , ' American ' , ' small ' , ' large ' , ' national ' , ' young ' , ' different ' , ' black ' , ' long ' , ' little ' , ' important ' , ' political ' , ' bad ' , ' white ' , ' real ' , ' best ' , ' right ' , ' senile ' , ' dead ' , ' public ' , ' sure ' , ' low ' , ' early ' , ' able ' , ' human ' , ' local ' , ' late ' , ' hard ' , ' major ' , ' better ' , ' economic ' , ' strong ' , ' possible ' , ' whole ' , ' free ' , ' military ' , ' true ' , ' federal ' , ' international ' , ' full ' , ' special ' , ' easy ' , ' clear ' , ' recent ' , ' certain ' , ' personal ' , ' open ' , ' red ' , ' difficult ' , ' available ' , ' likely ' , ' short ' , ' single ' , ' medical ' , ' current ' , ' wrong ' , ' private ' , ' past ' , ' foreign ' , ' fine ' , ' common ' , ' poor ' , ' natural ' , ' significant ' , ' similar ' , ' hot ' , ' dead ' , ' central ' , ' happy ' , ' serious ' , ' ready ' , ' simple ' , ' left ' , ' physical ' , ' general ' , ' environmental ' , ' financial ' , ' blue ' , ' democratic ' , ' dark ' , ' various ' , ' entire ' , ' close ' , ' legal ' , ' religious ' , ' cold ' , ' final ' , ' main ' , ' green ' , ' nice ' , ' huge ' , ' popular ' , ' traditional ' , ' cultural '];
var adjective = adjectives[Math.floor(Math.random()*adjectives.length)];
var presets = [ '1','2','1','2','3','4','5','6','5','6','7','8','9','10','11','12','11','12','13','14','15','16'];
var preset = presets[Math.floor(Math.random()*presets.length)];
if (preset == "1"){
document.getElementById("quote").innerHTML = ""+fronttext+verb+"your"+noun+"."
} else if (preset == "2"){
document.getElementById("quote").innerHTML = ""+fronttext+verb+"a"+noun+"."
} else if (preset == "3"){
document.getElementById("quote").innerHTML = ""+fronttext+adverb+verb+"your"+noun+"."
} else if (preset == "4"){
document.getElementById("quote").innerHTML = ""+fronttext+adverb+verb+"a"+noun+"."
} else if (preset == "5"){
document.getElementById("quote").innerHTML = ""+fronttext+verb+"your"+pnoun+"."
} else if (preset == "6"){
document.getElementById("quote").innerHTML = ""+fronttext+verb+pnoun+"."
} else if (preset == "7"){
document.getElementById("quote").innerHTML = ""+fronttext+adverb+verb+"your"+pnoun+"."
} else if (preset == "8"){
document.getElementById("quote").innerHTML = ""+fronttext+adverb+verb+pnoun+"."
} else if (preset == "9"){
document.getElementById("quote").innerHTML = ""+fronttext+verb+"your"+adjective+pnoun+"."
} else if (preset == "10"){
document.getElementById("quote").innerHTML = ""+fronttext+verb+adjective+pnoun+"."
} else if (preset == "11"){
document.getElementById("quote").innerHTML = ""+fronttext+verb+"your"+adjective+noun+"."
} else if (preset == "12"){
document.getElementById("quote").innerHTML = ""+fronttext+verb+"a"+adjective+noun+"."
} else if (preset == "13"){
document.getElementById("quote").innerHTML = ""+fronttext+adverb+verb+"your"+adjective+noun+"."
} else if (preset == "14"){
document.getElementById("quote").innerHTML = ""+fronttext+adverb+verb+"a"+adjective+noun+"."
} else if (preset == "15"){
document.getElementById("quote").innerHTML = ""+fronttext+adverb+verb+adjective+pnoun+"."
} else if (preset == "16"){
document.getElementById("quote").innerHTML = ""+fronttext+adverb+verb+"your"+adjective+pnoun+"."
}
};
html {
height: 100%;
width: 100%;
}
button {
height : 100px;
width : 100px;
position : fixed;
top: 100px;
left: 50px;
}
img {
width: 150px;
height: 150px;
object-fit: cover;
object-position: 25% 25%
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Inspirtaitonal quotesd genbaerator</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script src="script.js"></script>
<div id="image">
<p id="quote"></p>
<button onclick="qm()">Make a quote</button>
</body>
</html>
在這種情況下,您可以在dom加載后加載腳本
注意-你的div標簽沒有關閉,所以我在這里關閉了它
<body>
<div id="image"></div>
<p id="quote"></p>
<button onclick="qm()">Make a quote</button>
<script src="script.js"></script>
</body>
或者 您可以添加& quot延期& quot屬性添加到腳本標簽,如
<head>
<script defer src="script.js"></script>
</head>
<body>
<!-- Your Code Here -->
</body>