Chrome声称未定义简单功能 [英] Chrome claims simple function is undefined

查看:95
本文介绍了Chrome声称未定义简单功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在业余时间做了一个摩尔斯电码翻译器,并且第一次在Chrome中试用了它.调用translate()函数时,Chrome将Uncaught TypeError: translate is not a function at HTMLInputElement.onclick (morse:28)丢在我的脸上.其他功能按预期工作,可以从控制台调用translate()而不出现问题.此外,它在Firefox中也能完美运行.

I've made a little morse code translator in my free time, and just tried it out in Chrome for the first time. When the translate() function is called, Chrome throws Uncaught TypeError: translate is not a function at HTMLInputElement.onclick (morse:28) in my face. The other fuctions work as expected, and translate() can be called from the console without issue. In addition, it works perfectly in Firefox.

相关(我认为)HTML:

Relevant (I think) HTML:

<div id="mainspan">

    <form id="morseForm">
        <textarea id="morseInput" rows="8" cols="50"></textarea><br><br>

        <input type="radio" name="trans" id="mtt" value="morseToText"><label for="mtt">Morse &rarr; text</label><br>
        <input type="radio" name="trans" id="ttm" value="testToMorse"><label for="ttm">Text &rarr; morse</label><br>
        <input type="button" onclick="translate()" value="Translate"><br><br>

        <textarea id="morseOutput" rows="8" cols="50"></textarea><br>
    </form>

</div>

还有JS:

function translate() {
    if (document.getElementById("mtt").checked == true) {morseToText()};
    if (document.getElementById("ttm").checked == true) {textToMorse()};
}

推荐答案

这可能是Chrome的不需要的功能",translate似乎有一些特殊含义"事件,尽管通常没有定义

This is probably an "unwanted feature" of Chrome, translate seems to have some "special meaning" event though it is usually undefined

<script>
  function translate () {
    alert('or nah');
  }
</script>
<button onclick="translate()">
  translate!
</button>

但是无效

<script>
  function translate () {
    alert('or nah');
  }
  window.onload = function() {
    document.querySelector('button').addEventListener('click', translate);
  }
</script>
<button>
  translate!
</button>

有效-本质上相同,但显然不一样

works - essentially the same, but obviously not

最简单的解决方案

  1. 使用addEventListener方法将click事件添加到按钮,或者
  2. 不要使用名称translate

这篇关于Chrome声称未定义简单功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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