2009-09-01 52 views
0

我刚刚开始学习JS,并且当然有一些困难。我通常很快把握事情,但我不能为我的生活找到解决办法。我想了解为什么会发生这种情况。JS:通过document.write显示HTML w /变量

我的目标是使用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>"); 
       } 
} 

回答

2

好了,之前,我们甚至开始尝试合作,通过怎样if语句去工作,我们需要修复的事实,你是分配没有比较在你的if()语句中。

而不是在你的if语句中有一个= sign,你需要有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语句在稍后的日期:)

+0

感谢Steerpike(和安德烈斯) 确定。我现在已经完成了这个工作,不过,在提交第三个提示后,我仍然遇到同样的最终结果。 – 2009-09-01 19:26:26

+0

真棒,它完美的作品..谢谢你Steerpike ..伤心地在这一整天。现在我终于可以放下头来休息了。 非常感谢! (并感谢你们其他人为了鸣叫) – 2009-09-01 19:55:40

0

替换“如果(site_type =网站){”为“如果(site_type ==网站){“ 和其他。

0

除了分配VS从Steerpike和安德烈斯比较的问题,你会因为你是比较你的变量site_type到另一个变量遇到问题:websitevideoimagearticle。这些都是未初始化的变量,因此,有条件的将实际检查,如果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){ 
... 
} 
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错误是什么,并提供了很多其他工具。

+0

好的。甜。现在我终于得到了一个输出!除此之外,我还没有找到。 “site_title”和“site_url”的值保持不变。嗯... – 2009-09-01 19:50:05