简体   繁体   English

JS函数没有被调用?

[英]JS function not being called?

im a complete nooblet at javascript so if some one could help me id appreciate it :) 我在javascript上是一个完整的nooblet,所以如果有人可以帮助我id欣赏它:)

im trying to call my function (questions) from my external js file however its not loading it for what ever reason.any help would be awesome! 我试图从我的外部js文件中调用我的函数(问题),但是由于任何原因它都没有加载它。任何帮助都将很棒!

HTML file HTML文件

 <section>

    <h3 id="q1" window.onload= "loadQuestion1()" style="color:blue"></h3>
    <ul>
        <li><input type="radio" id="answers" value="Css is 1" >Answer 1</li>
        <li><input type="radio">Answer 2</li>
        <li><input type="radio">Answer 3</li>
        <li><input type="radio">Answer 4</li>
    </ul>

    </section>
</div>

<footer>
</footer>
<script type="text/javascript" src="js/quiz.js"></script>
</body>

javascript file javascript文件

console.log("53");

var questions = Array["question 1", "question 2", "question 3","question 4" ]

function loadQuestion1(){
console.log("q1");
document.getElementById('q1').innerHTML = "text";
}
function rand(min, max) { 

var offset = min; 
var range = (max - min) + 1; 
var randomNumber = Math.floor( Math.random() * range) + offset; 
return randomNumber; } 

randomNumber = rand(0, questions.length - 1); 
randomQuestions = questions.[randomNumber];

So currently it should load the word "text" but its not loading that for some reason - never mind the actual question. 因此,当前它应该加载“文本”一词,但是由于某种原因它不加载该词-不用管实际的问题。

ps im trying to make a small quiz app which selects a random question from the array ps im试图制作一个小的测验应用程序,该应用程序从数组中选择一个随机问题

window.onload is a JavaScript property, not an HTML attribute. window.onload是JavaScript属性,而不是HTML属性。

HTML attributes do not have a . HTML属性没有. in them. 在他们中。

There is no window attribute in HTML. HTML中没有window属性。

There is no onload attribute for an h3 element (since it doesn't trigger the loading of any content). h3元素没有onload属性(因为它不会触发任何内容的加载)。

If you were writing code in the 90s you would: 如果您在90年代编写代码,那么您将:

<body onload="loadQuestion1()">

Code from the 00s would typically use: 从00开始的代码通常使用:

window.onload = loadQuestion1;

Code from this decade typically uses: 这十年的代码通常使用:

addEventListener('load', loadQuestion1);

You should delete code window.onload= "loadQuestion1()" from h3 tag and add onload= "loadQuestion1()" to body tag. 您应该从h3标记中删除代码window.onload =“ loadQuestion1()”,然后在正文标记中添加onload =“ loadQuestion1()”。

This should work. 这应该工作。

This is working code, you had also few syntax error in JS, please compare with your code 这是有效的代码,您在JS中的语法错误也很少,请与您的代码进行比较

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body onload="loadQuestion1()">
<section>
    <h3 id="q1" style="color:blue"></h3>
    <ul>
        <li><input type="radio" id="answers" value="Css is 1">Answer 1</li>
        <li><input type="radio">Answer 2</li>
        <li><input type="radio">Answer 3</li>
        <li><input type="radio">Answer 4</li>
    </ul>
</section>
<footer>
</footer>
<script type="text/javascript">
    console.log("53");

    var questions = Array["question 1", "question 2", "question 3", "question 4" ];

    function loadQuestion1() {
        console.log("q1");
        document.getElementById('q1').innerHTML = "text";
    }
    function rand(min, max) {

        var offset = min;
        var range = (max - min) + 1;
        var randomNumber = Math.floor(Math.random() * range) + offset;
        return randomNumber;
    }

    randomNumber = rand(0, questions.length - 1);
    randomQuestions = questions[randomNumber];
</script>
</body>
</html>

Just put your JS in this: 只需将您的JS放在此:

window.onload = function(){
    /*YOUR JS HERE*/
}

The window.onload event start when the document's window is ready 当文档窗口准备就绪时,window.onload事件开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM