Javasctipt函数在运行时没有被调用? [英] Javasctipt function running without being called?

查看:69
本文介绍了Javasctipt函数在运行时没有被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的page.js文件中只有6行代码,它们是:

So I only have 6 lines of code in my page.js file and they are:

document.getElementById("desired").addEventListener("blur", gradeChange());
document.getElementById("calculate").addEventListener("click", gradeChange());

function gradeChange() {                                                              
    var dog = document.getElementById("desired").value;                               
    console.log(dog);                                                                 
}

在我的page.html中,我有:

And in my page.html I have:

<input id="desired" type="text">

和其他地方:

<button id="calculate" type="button">Calculate</button>

我认为无需按钮即可使用,因此当我在文本框中键入内容然后离开文本框时,它将显示在日志中.但是什么都没显示,所以我添加了按钮以使其更直接.尽管如此,它在日志中什么也没做.知道为什么什么都没出现吗?

I thought it would work without the button, so that when I typed something in the textbox and then left the textbox, it would show up in the log. But nothing showed up, so I added the button to make it more straight forward. Still, it did nothing in the log. Any idea why nothing is showing up?

推荐答案

gradeChange()调用函数gradeChange

gradeChange() calls the function gradeChange

document.getElementById("desired").addEventListener("blur", gradeChange());
document.getElementById("calculate").addEventListener("click", gradeChange());

将gradeChange()更改为gradeChange

Change gradeChange() to gradeChange

document.getElementById("desired").addEventListener("blur", gradeChange);
document.getElementById("calculate").addEventListener("click", gradeChange);

这篇关于Javasctipt函数在运行时没有被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆