简体   繁体   English

如何链接HTML中的JavaScript程序并使其运行?

[英]How do I link a JavaScript Program in HTML, and Get It To Run?

Please be patient throughout this, I am VERY bad at coding both JavaScript and HTML, and this problem is probably trivial to anyone who knows anything. 在整个过程中请耐心等待,我对JavaScript和HTML都非常不好,这个问题对于任何不了解的人来说都是微不足道的。

For a school project, I have to create a website with a JavaScript, interactive feature. 对于学校项目,我必须创建一个具有JavaScript交互式功能的网站。 I'm planning on making a button that subsequently, when pressed, prompts the user many questions, such as "Do you have an account?", "Username?", and "Password". 我打算创建一个按钮,随后在按下该按钮时,会提示用户许多问题,例如“您是否有帐户?”,“用户名?”和“密码”。 I believe my JavaScript is written correctly, but when I link it in my HTML, and when I press the button that is intended to start this prompting, nothing happens. 我相信我的JavaScript编写正确,但是当我将其链接到HTML中时,以及当我按下旨在启动此提示的按钮时,都不会发生任何事情。 What am I doing wrong? 我究竟做错了什么?

Here are segments of both my HTML and JavaScript code that I am using. 这是我正在使用的HTML和JavaScript代码的片段。

JavaScript JavaScript的

function LogIn(){
var Account = prompt("Do you have an account?")

if (Account = "yes"){
    var Username = prompt("Username");
    if (Username = "MarkColley"){
        var Password = prompt("Password");

... ...

else{
    confirm("That keyword is not acceptable. Try 'yes' or 'no' instead.");
}
document.getElementById("demo").innerHTML = text;
}

HTML HTML

<li><a href="CurrentShows.html">Current Shows</a></li>
<li><img src="https://cdn4.iconfinder.com/data/icons/man-and-woman/154/man-human-person-login-512.png" style="width:20px;height:20px;"><button onclick="Login()">Sign Up/Log In</button></a></li>
<script type="text/javascript" src="LogInSignUp.js"></script>
</ul>

If it helps at all, here is an image of what the button/area that I am trying to get to work looks like. 如果有帮助的话,这是我尝试使用的按钮/区域的外观的图像。

An image of my unworking, useless button. 我无法使用的按钮的图片。

Here's a fiddle with a fixed version of your code, now it works. 这是一个带有固定版本代码的小提琴,现在可以使用了。

http://jsfiddle.net/e3xpy5tq/ http://jsfiddle.net/e3xpy5tq/

What didn't work: 什么不起作用:

  1. You used assignment instead of comparison (= instead of ==). 您使用分配而不是比较 (=而不是==)。

  2. You missed a semicolon and a couple curly brackets . 您错过了分号和几个花括号

  3. Your function LogIn was misspelled on the HTML ( Login instead of LogIn ; JavaScript is case-sensitive). 您的函数LogIn在HTML上拼写错误Login而不是LogIn ; JavaScript区分大小写)。

     function LogIn() { var Account = prompt("Do you have an account?"); if (Account == "yes") { var Username = prompt("Username"); if (Username == "MarkColley") { var Password = prompt("Password"); } else { confirm("That keyword is not acceptable. Try 'yes' or 'no' instead."); } document.getElementById("demo").innerHTML = text; } } 

A couple suggestions: 几个建议:

As mentioned in some comments, go through JavaScript and HTML tutorials. 如某些评论中所述,请阅读JavaScript和HTML教程。 Also, use a code linter in your favourite text editor. 另外,在您喜欢的文本编辑器中使用代码填充器。 I use Sublime Text with one of its many plugins. 我使用Sublime Text及其众多插件之一。 It will help you find your errors by yourself. 这将帮助您自己发现错误。

To make the function fire you are to go this way: 要使该函数触发,请按照以下方式操作:

<a href="#!" onclick="LogIn()">Click</a>

And to check if a value is equal to another one, you are to use the equal signs: 要检查一个值是否等于另一个,请使用等号:

if(value_one === value_two)....

Because if you use the single equal sign, you are actually assigning that value to a variable. 因为如果使用单个等号,则实际上是将该值分配给变量。

So if you go: 因此,如果您去:

if (Account = "yes"){...

You are assigning to Account the value of yes . 您正在为Account分配yes的值。

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

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