gpt4 book ai didi

javascript - JS : Displaying html w/variables via document. 写入

转载 作者:行者123 更新时间:2023-12-02 20:45:21 24 4
gpt4 key购买 nike

前几天我刚刚开始学习 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>");
}
}

最佳答案

好吧,在我们开始尝试了解 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 语句放在哪里:)

关于javascript - JS : Displaying html w/variables via document. 写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1364109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com