JS:通过 document.write 显示带有变量的 html

发布于 2024-08-03 08:53:42 字数 1262 浏览 1 评论 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>");
                }
}

I just started learning JS the other day and am (of course) having some difficulty. I usually grasp onto things quickly but I can't for the life of my find a solution to this. I'd like to understand why this is happening.

My objective is to use 3 prompt boxes, all which come one after another, to print out a piece of html code, which will be a simple URL. I'll add more to this but I'd like to get past this first.

At the moment, I'm getting the prompts, but after I enter data into the third box and submit it, nothing happens.

What am I doing wrong here? If it's an error with my document.write code, what are some things I should be looking out for?

Thanks!..

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>");
                }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

oО清风挽发oО 2024-08-10 08:53:42

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

if 语句中不需要有一个 = 号,而是需要有 2 个等号。像这样:

if (site_type == website)

单个 = 符号用于分配变量,因此在您的情况下,您实际上是将 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 语句放在哪里:)

Well, before we even start trying to work through how the if statements are going to work, we need to fix the fact that you are assigning not comparing in your if() statements.

Rather than have a single = sign in your if statements you need to have 2 equal signs. Like this:

if (site_type == website)

A single = sign is used to assign variables, so in your case you're actually assigning the value of website into the variable of site_type - not comparing the two seperate values.

Try this:

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>");
        }
}

I've made a few changes to your code in the above chunk, not just removing the else statements. I've also made the if() comparisons check against strings rather than against empty variables as you had before and I've changed the document.write functions to use the added prompt strings in them.
If we can get that to work, we can start thinking about where we really want to put the else statements at a later date :)

老街孤人 2024-08-10 08:53:42

将“if (site_type = website) {”替换为“if (site_type == website) {”
和其他人。

replace "if (site_type = website) {" for "if (site_type == website) {"
and others.

一城柳絮吹成雪 2024-08-10 08:53:42

除了 Steerpike 和 Andres 的赋值与比较问题之外,您还会遇到问题,因为您将变量 site_type 与另一个变量:website视频图像文章。这些是未初始化的变量,因此条件实际上将检查 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){
...
}

In addition to the assignment vs comparison problem from Steerpike and Andres, you are going to run into problems because you are comparing your variable site_type to another variable: website or video or image or article. These are uninitialized variables, so the conditional will actually be checking if site_type == null

What was happening in your original code is the following:

// 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){
...
}
盛夏已如深秋| 2024-08-10 08:53:42

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

例如:

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 错误是什么,并提供许多其他信息工具。

In addition to using assignment instead of comparison operator, you should probably also quote your strings.

eg:

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

Unless those are actually meant to be variables, video, will produce an error if it is undefined, thus your JavaScript stops.

If you're not using Firefox, download it and install Firebug.
http://getfirebug.com/

It will let you know what the JavaScript errors are, and offer a lot of other tools.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文