0

前几天我刚开始学习 JS,我(当然)遇到了一些困难。我通常会很快掌握事情,但我无法终生找到解决方案。我想了解为什么会这样。

我的目标是使用3个提示框,依次出现,打印出一段html代码,这将是一个简单的URL。我会添加更多内容,但我想先解决这个问题。

目前,我收到了提示,但是在我将数据输入第三个框并提交后,什么也没有发生。

我在这里做错了什么?如果我的 document.write 代码有错误,我应该注意哪些事项?

谢谢!..

function show_prompt()
{
    var site_type = prompt("What kind of link is this?");
    var site_url = prompt("What is the URL?");
    var site_title = prompt("Give the link a title");
    if (site_type = website) {
        document.write("<a style=\"color: #777777\" href=\"http:\/\/site_url\" title=\"site_title\">site_title<\/a>");
    }
    else 
        if (site_type = video) {
            document.write("<a style=\"color:#98B2C3\" href=\"http:\/\/site_url\" title=\"site_title\">site_title<\/a>");
        }
        else 
            if (site_type = image) {
                document.write("<a style=\"color:#8D5359\" href=\"http:\/\/site_url\" title=\"site_title\">site_title<\/a>");
            }
            else 
                (site_type = article); {
                 document.write("<a style=\"color:#768D53\" href=\"http:\/\/site_url\" title=\"site_title\">site_title<\/a>");
                }
}
4

4 回答 4

2

好吧,在我们开始尝试了解 if 语句如何工作之前,我们需要解决您在 if() 语句中分配而不是比较的事实。

与其在 if 语句中使用一个 = 符号,不如使用 2 个等号。像这样:

if (site_type == website)

单个 = 符号用于分配变量,因此在您的情况下,您实际上是将网站的值分配给 site_type 的变量 - 而不是比较两个单独的值。

试试这个:

function show_prompt()
{
        var site_type = prompt("What kind of link is this?");
        var site_url = prompt("What is the URL?");
        var site_title = prompt("Give the link a title");
        if (site_type == "website") {
            document.write("<a style=\"color: #777777\" href=\"http:\/\/"+site_url+"\" title=\"site_title\">"+site_title+"<\/a>");
        }
        if (site_type == "video") {
            document.write("<a style=\"color:#98B2C3\" href=\"http:\/\/"+site_url+"\" title=\"site_title\">"+site_title+"<\/a>");
        }
        if (site_type == "image") {
            document.write("<a style=\"color:#8D5359\" href=\"http:\/\/"+site_url+"\" title=\"site_title\">"+site_title+"<\/a>");
        }
        if(site_type == "article") {
            document.write("<a style=\"color:#768D53\" href=\"http:\/\/"+site_url+"\" title=\"site_title\">"+site_title+"<\/a>");
        }
}

我在上面的代码块中对您的代码进行了一些更改,而不仅仅是删除了 else 语句。我还对字符串进行了 if() 比较检查,而不是像以前那样检查空变量,并且我更改了 document.write 函数以使用其中添加的提示字符串。如果我们可以让它发挥作用,我们就可以开始考虑我们真正想在以后将 else 语句放在哪里 :)

于 2009-09-01T19:12:36.953 回答
0

将“if (site_type = website) {”替换为“if (site_type == website) {”等。

于 2009-09-01T19:13:11.087 回答
0

除了 Steerpike 和 Andres 的赋值与比较问题之外,您还会遇到问题,因为您将变量site_type与另一个变量进行比较:websiteorvideoimageor article。这些是未初始化的变量,因此条件实际上将检查是否site_type == null

您的原始代码中发生的情况如下:

// The value on the right side of the = is assigned to site_type.
// All of these are uninitialized, so will evaluate to null
// Since null is falsy, each conditional fails
if (site_type = website){ 
...
} else if (site_type = video){
...
} else if (site_type = image){
...
} else if (site_type = article){
...
}
于 2009-09-01T19:27:47.950 回答
0

除了使用赋值而不是比较运算符之外,您可能还应该引用您的字符串。

例如:

if (site_type == 'video') {
                        document.write("<a style=\"color:#98B2C3\" href=\"http:\/\/site_url\" title=\"site_title\">site_title<\/a>");
                }

除非它们实际上是变量,video否则如果未定义,则会产生错误,因此您的 JavaScript 会停止。

如果您不使用 Firefox,请下载并安装 Firebug。 http://getfirebug.com/

它将让您知道 JavaScript 错误是什么,并提供许多其他工具。

于 2009-09-01T19:40:55.320 回答