[英]What's wrong with this JavaScript string?
I'm a JavaScript newbie, just doing a little script so that I can rotate an image + link every few days. 我是JavaScript的新手,只做了一个小脚本,这样我就可以每隔几天旋转一次图片+链接。 I'm 90% sure my error is in the bottom line of code - seems to be something to do with the way quotes are used (which is confusing me a lot).
我有90%的把握是我的错误出在代码的最后一行-似乎与使用引号的方式有关(这使我感到非常困惑)。
Correct code + explanation of where and why to use quotes would be much appreciated! 正确的代码+关于在何处以及为什么使用引号的说明将不胜感激!
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
today = new Date();
date = today.getDate();
date = date - date%9;
date = date/9;
date = date%3;
arday = new Array("image 1", "image 2", "image 3");
linkday = new Array("link 1", "link 2", "link 3");
document.write("<a href= '" + linkday[date] + target="_blank" ><img src='" + arday[date] "'/> </a>");
// End -->
</SCRIPT>
You need to escape some quotes and add some other ones in your document.write
statement: 您需要转义一些引号,并在
document.write
语句中添加其他一些引号:
document.write("<a href='" + linkday[date] + "' target='_blank'><img src='" + arday[date] "' /></a>");
When you use variables, you should also use var
to prevent them from automatically attaching to the global namespace. 使用变量时,还应使用
var
防止它们自动附加到全局名称空间。 In this case, there is no visible difference, but there would be a difference if the above code was in a function. 在这种情况下,没有可见的区别,但是如果上面的代码在函数中,则会有区别。 So the following variable assignments would be preferred:
因此,以下变量分配将是首选:
var today = new Date();
var date = today.getDate();
date = date - date % 9;
date /= 9;
date %= 3;
var arday = ["image 1", "image 2", "image 3"];
var linkday = ["link 1", "link 2", "link 3"];
By the way, the proper tag to use is 顺便说一句,正确使用的标签是
<script type="text/javascript">
language
has been deprecated. language
已弃用。 The type
attribute can be omitted in HTML5, however, if that is the doctype that you are using. 但是,如果您使用的是doctype,则可以在HTML5中省略
type
属性。
Also, the comments 另外,评论
<!-- Begin
...
// End -->
can be removed, as they don't serve much of a purpose in modern-day browsers, and actually does comment out the script if a browser is fully following XHTML. 可以删除,因为它们在现代浏览器中没有太多用处,并且如果浏览器完全遵循XHTML,则实际上会注释掉脚本。
Go from the correct string and add things in 从正确的字符串中添加内容
<a href="" target="_blank" ><img src=""/></a>
Wrap it in single quotes since the string above is using double quotes. 因为上面的字符串使用双引号,所以将其用单引号引起来。
'<a href="" target="_blank" ><img src=""/></a>'
Add the first variable reference 添加第一个变量引用
'<a href="' + linkday[date] + '" target="_blank" ><img src=""/></a>'
Add the second variable 添加第二个变量
'<a href="' + linkday[date] + '" target="_blank" ><img src="' + arday[date] + '"/></a>'
wrap it in the document.write 将其包装在document.write中
document.write('<a href="' + linkday[date] + '" target="_blank" ><img src="' + arday[date] + '"/></a>');
language
attribute is ignored by browsers, remove it. language
属性被浏览器忽略,将其删除。 Even the type
attribute can be omitted. type
属性。 <!-- -->
<!-- -->
new Array()
new Array()
var
when declaring variables var
var
and a series of variable declarations. var
和一系列变量声明。 ""
for attribute values. ""
用作属性值。 <
and the tag name. <
和标签名称之间不得有空格。 Here's a cleaned code : 这是一个干净的代码 :
<script>
var today = new Date(),
date = today.getDate(),
arday = ["image 1", "image 2", "image 3"],
linkday = ["link 1", "link 2", "link 3"];
date = date - date % 9;
date = date / 9;
date = date % 3;
document.write('<a href="' + linkday[date] + '" target="_blank" ><img src="' + arday[date] + '" / ></a>');
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.