简体   繁体   English

我很难弄清楚我的html和js文件出了什么问题。 我需要数元音

[英]I am having a hard time figuring out what is wrong with my html and js file. I need to count the vowels

I need to fix this segment of code for a class and I have fixed a number of things but I'm not sure why it doesn't work. 我需要为一个类修复这段代码,并且已经修复了许多问题,但是我不确定为什么它不起作用。 It is supposed to count the number of vowels in the phrase and return them as an alert. 应该计算出短语中的元音数量,并将其作为警报返回。 When I click on the button nothing appears. 当我点击按钮时,什么也没有出现。

Here's the html. 这是html。 It works fine but added in case I am missing something 它可以正常工作,但在我丢失某些东西的情况下可以添加

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Vowels</title>   
    <link rel="stylesheet" href="../css/easy.css">      
    <script src="p3-vowels.js"></script>
</head>
<body>
    <header>
         <h1>Count Vowels</h1>

    </header>
    <main>
        <label>Type a phrase here:
            <input type='text' id='textBox'></input>
        </label>
        <button id='countBtn' type='button'>
           Count Vowels (a,e,i,o,u)</button>
        <div id='outputDiv'></div>
    </main>
</body>

</html>

here is the JS. 这是JS。 It doesn't seem to register the "on.click" at the end of the code 它似乎没有在代码末尾注册“ on.click”

function countVowels() {
    var textBox, phrase, i, pLength, letter, vowelCount;
    textBox = document.getElementById('textBox');
    phrase = textBox.value;
    phrase = phrase.toLowerCase();
    vowelCount = 0;
    for (i = 0; i < phrase.length; i += 1) {
        letter = phrase[i];
        if (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u') {
            vowelCount++;
        }
    }

    alert(vowelCount + ' vowels');
    var outArea = document.getElementById('outputDiv');
    outArea.innerHTML = vowelCount + ' vowels in ' + phrase;
}

function init() {
    alert('init vowels');
    var countTag = document.getElementById('countBtn');
    countTag.onclick = countVowels;
}

window.onload = init();

The problem is window.onload = init(); 问题是window.onload = init(); , here you are calling init method and is assigning the value returned by init as the onload callback. ,这里您正在调用init方法,并将init返回的值分配为onload回调。 So when the init method is called the countBtn is not yet added to the dom resulting in an error like Uncaught TypeError: Cannot set property 'onclick' of null . 因此,当调用init方法时, countBtn尚未添加到dom中,从而导致类似Uncaught TypeError: Cannot set property 'onclick' of null的错误Uncaught TypeError: Cannot set property 'onclick' of null

What you really want is to call init on the load event, so you need to pass the reference to init function to onload 您真正想要的是在load事件上调用init ,因此您需要将对init函数的引用传递给onload

It should be 它应该是

window.onload = init;

Demo: Fiddle 演示: 小提琴

暂无
暂无

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

相关问题 我很难弄清楚如何动态设置时间线 - I am having a hard time figuring out how to dynamically setup a timeline 我的 Js 文件没有链接到我的 html。我做错了什么? - My Js file is not linking to my html. What am i doing wrong? Codecademy让我很难过,我做错了什么? - Codecademy is giving me a hard time, what am I doing wrong? 我无法在 .js 文件中更改 function 名称。 怎么了? - I can not change the function name in .js file. What is wrong? 我在使用 HTML 和 JS 提取用户输入并将其写入页面时遇到问题。 你能指出我在这里做错了什么吗? - I am having a problem pulling the user's input and writing it into a page with HTML and JS. Are you able to point out what I am doing wrong here? CSS-需要帮助找出下拉菜单导航出错的地方 - CSS - Need help figuring out where I went wrong with my dropdown menu nav 我无法弄清楚我的自定义挂钩中的逻辑 - I’m having trouble figuring out the logic in my custom hook 在flickr上将CSS选择器与beautifulsoup结合使用时遇到困难,我做错了吗? - Having a hard time using CSS selector with beautifulsoup on flickr, am i doing something wrong? 我正在尝试在Codepen中构建一个React-Redux&#39;Markdown Previewer&#39;,但是在弄清楚应该做什么方面有很多麻烦 - I'm trying to build a React-Redux 'Markdown Previewer' in codepen, but am having lots of trouble figuring out what is supposed to be doing what 我使用Pixastic的HTML5 Canvas和Javascript过滤器处理图像时遇到问题,我在做什么错? - I am having trouble processing my image using HTML5 Canvas and Javascript Filters from Pixastic, what am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM