简体   繁体   English

Javascript onclick函数未执行

[英]Javascript onclick function not executing

JavaScript noob here, I have a simple piece of code, but my onclick event that calls upon a function I have defined is not executing. JavaScript noob在这里,我有一段简单的代码,但是调用我定义的函数的onclick事件没有执行。 Here's the code: 这是代码:

<!DOCTYPE html>
<html>
<head>
<title>
    window.navigator
    <script>
    function thankYou(){
       alert("Thanks for Checking!");
    }
    </script>    
</title>
<style>
    td{border:1px solid gray;padding:3px 5px;}
</style>
</head>
<body>
<script>
    document.write("<table>");
    document.write("<tr><td>appName</td><td>"+navigator.appName+ </td></tr>");
    document.write("<tr><td>appCodeName</td><td>"+navigator.appCodeName+"</td></tr>");
    document.write("<tr><td>appVersion</td><td>"+navigator.appVersion+"</td></tr>");
    document.write("<tr><td>language</td><td>"+navigator.language+"</td></tr>");
    document.write("<tr><td>cookieEnabled</td><td>"+navigator.cookieEnabled+"</td></tr>");
    document.write("<tr><td>cpuClass</td><td>"+navigator.cpuClass+"</td></tr>");
    document.write("<tr><td>onLine</td><td>"+navigator.onLine+"</td></tr>");
    document.write("<tr><td>platform</td><td>"+navigator.platform+"</td></tr>");
    document.write("<tr><td>No of Plugins</td><td>"+navigator.plugins.length+"</td></tr>");
    document.write("</table>");
</script>
<p>
    <input type="button" name="Thanks for checking!" value="Click me!" onClick="thankYou()"/>
</p>
</body>
</html>

Can anyone explain what I'm doing wrong? 谁能解释我在做什么错? Thank you! 谢谢!

You have a typo. 你有错字 Missing a " on the first document write line. Also the script tag should not be in the title tag. 在第一个文档写入行上缺少" 。”而且script标签不应该在title标签中。

document.write("<tr><td>appName</td><td>"+navigator.appName+ </td></tr>");
                                                            ^^^

You were missing a quote and needed to move your script tag, see working example below. 您缺少引号,需要移动脚本标记,请参见下面的工作示例。

 <!DOCTYPE html> <html> <head> <title> window.navigator </title> <style> td { border: 1px solid gray; padding: 3px 5px; } </style> </head> <body> <script> function thankYou() { alert("Thanks for Checking!"); } </script> <script> document.write("<table>"); document.write("<tr><td>appName</td><td>" + navigator.appName + "</td></tr>"); document.write("<tr><td>appCodeName</td><td>" + navigator.appCodeName + "</td></tr>"); document.write("<tr><td>appVersion</td><td>" + navigator.appVersion + "</td></tr>"); document.write("<tr><td>language</td><td>" + navigator.language + "</td></tr>"); document.write("<tr><td>cookieEnabled</td><td>" + navigator.cookieEnabled + "</td></tr>"); document.write("<tr><td>cpuClass</td><td>" + navigator.cpuClass + "</td></tr>"); document.write("<tr><td>onLine</td><td>" + navigator.onLine + "</td></tr>"); document.write("<tr><td>platform</td><td>" + navigator.platform + "</td></tr>"); document.write("<tr><td>No of Plugins</td><td>" + navigator.plugins.length + "</td></tr>"); document.write("</table>"); </script> <p> <input type="button" name="Thanks for checking!" value="Click me!" onClick="thankYou()" /> </p> </body> </html> 

You cannot have a script tag in the <title> . 您不能在<title>包含脚本标签。 Your browser most likely will display the text verbatim in its title bar, because that's what it is supposed to do. 您的浏览器很可能会在其标题栏中逐字显示文本,因为这是应该做的。 Some browsers have (or had) problems with extra white spaces in the title, such as newlines. 某些浏览器在标题中有多余的空格,例如(换行符),但有(或有过)问题。 Better strip them. 最好脱掉它们。

And throw away your book, it must be really really old. 扔掉你的书,它必须真的很旧。 Do not use document.write(...) , forget that this function exists. 不要使用document.write(...) ,请记住该函数存在。 Do not use <tag onevent="..."> , but proper event listeners . 不要使用<tag onevent="..."> ,而是使用适当的事件侦听器 Also read the console output (shift+ctrl+k or F12) to find typos in your JavaScript code. 还要阅读控制台输出(shift + ctrl + k或F12),以在JavaScript代码中找到错别字。

A nice overview for primers in different programming languages: here . 有关使用不同编程语言编写的入门的概述,请参见此处 (The title of the site might be offensive, though. :) ) (不过,该网站的标题可能令人反感。:))

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

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