Javascript 在 JS Fiddle 中工作,但不在我的 Dreamweaver 环境中工作(通过浏览器 Safari 或 Opera 或实时视图中的预览)

发布于 2025-01-08 16:02:44 字数 2567 浏览 0 评论 0原文

我在网上发现了这个脚本,并对它进行了轻微的修改,以定制的方式打印出当前的日期和时间。它在 JS Fiddle http://jsfiddle.net/PVZZ8/ 中工作正常,但在我的 HTML 文档中却不起作用不做某事。它在实时视图中不起作用,在 Safari 或 Opera 中预览页面时也不起作用。我检查了语法,一切似乎都很好。我使用的是 Dreamweaver CS5。我使用了一些在 W3 Schools 上找到的较短脚本,它们工作得很好,但我对 Javascript 还很陌生,所以也许我在使用的较长脚本中缺少一些通用步骤? HTML 文档如下所示:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js">
</script>
<script type="text/javascript">
function getCalendarDate()
{
   var months = new Array(13);
   months[0]  = "JAN";
   months[1]  = "FEB";
   months[2]  = "MAR";
   months[3]  = "APR";
   months[4]  = "MAY";
   months[5]  = "JUN";
   months[6]  = "JUL";
   months[7]  = "AUG";
   months[8]  = "SEP";
   months[9]  = "OCT";
   months[10] = "NOV";
   months[11] = "DEC";
   var now         = new Date();
   var monthnumber = now.getMonth();
   var monthname   = months[monthnumber];
   var monthday    = now.getDate();
   var year        = now.getYear();
   if(year < 2000) { year = year + 1900; }
   var dateString = monthday +
                    ' ' +
                    monthname +
                    ' ' +
                    year;
   return dateString;
} 

function getClockTime()
{
   var now    = new Date();
   var hour   = now.getHours();
   var minute = now.getMinutes();
   var second = now.getSeconds();
   var ap = "AM";
   if (hour   > 11) { ap = "PM";             }
   if (hour   > 12) { hour = hour - 12;      }
   if (hour   == 0) { hour = 12;             }
   if (hour   < 10) { hour   = "0" + hour;   }
   if (minute < 10) { minute = "0" + minute; }
   if (second < 10) { second = "0" + second; }
   var timeString = hour +
                    ':' +
                    minute +
                    ':' +
                    second +
                    " " +
                    ap;
   return timeString;
}

var calendarDate = getCalendarDate();
var clockTime = getClockTime();
window.onload = document.write('<p>' + 'Your fortunes as of' + ' ' + calendarDate + ' ' + ' ' + 'at' + ' ' + clockTime + '</p>');​​​​​​​​​​
</script>

</head>

<html>
<body>


</body>
</html> 

I came across the script online and have slightly modified it to print out the current date and time in a customised manner. It works fine in JS Fiddle http://jsfiddle.net/PVZZ8/ but in my HTML document it doesn't do a thing. It won't work in Live View nor when I preview the pages in Safari or Opera. I have checked over the syntax and all seems good. I am using dreamweaver CS5. I have used some shorter scripts I found on W3 Schools and they work fine but I am quite new to Javascript so perhaps there is some general step I am missing in the longer script I am using? The HTML document looks like this:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js">
</script>
<script type="text/javascript">
function getCalendarDate()
{
   var months = new Array(13);
   months[0]  = "JAN";
   months[1]  = "FEB";
   months[2]  = "MAR";
   months[3]  = "APR";
   months[4]  = "MAY";
   months[5]  = "JUN";
   months[6]  = "JUL";
   months[7]  = "AUG";
   months[8]  = "SEP";
   months[9]  = "OCT";
   months[10] = "NOV";
   months[11] = "DEC";
   var now         = new Date();
   var monthnumber = now.getMonth();
   var monthname   = months[monthnumber];
   var monthday    = now.getDate();
   var year        = now.getYear();
   if(year < 2000) { year = year + 1900; }
   var dateString = monthday +
                    ' ' +
                    monthname +
                    ' ' +
                    year;
   return dateString;
} 

function getClockTime()
{
   var now    = new Date();
   var hour   = now.getHours();
   var minute = now.getMinutes();
   var second = now.getSeconds();
   var ap = "AM";
   if (hour   > 11) { ap = "PM";             }
   if (hour   > 12) { hour = hour - 12;      }
   if (hour   == 0) { hour = 12;             }
   if (hour   < 10) { hour   = "0" + hour;   }
   if (minute < 10) { minute = "0" + minute; }
   if (second < 10) { second = "0" + second; }
   var timeString = hour +
                    ':' +
                    minute +
                    ':' +
                    second +
                    " " +
                    ap;
   return timeString;
}

var calendarDate = getCalendarDate();
var clockTime = getClockTime();
window.onload = document.write('<p>' + 'Your fortunes as of' + ' ' + calendarDate + ' ' + ' ' + 'at' + ' ' + clockTime + '</p>');​​​​​​​​​​
</script>

</head>

<html>
<body>


</body>
</html> 

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

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

发布评论

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

评论(1

差↓一点笑了 2025-01-15 16:02:44

当我复制并粘贴上面的代码并在 Dreamweaver、IE9 和 FIrefox 中厌倦它时,所有错误都出现了。我检查了 IE9 和 Firefox 的控制台,发现最后一行(document.write 行)末尾的分号后面有一个非法字符。在检查上面代码的源代码时,我发现最后一个分号后面有一个由 10 个“​”字符组成的字符串(零宽度空格)。不知道它们是如何到达那里的,但是一旦我将它们从代码中删除,该页面就开始在 IE9、Firefox 和 Dreamweaver 的实时视图中工作。

为此,我基本上必须选择尾随分号和开头 <字符,删除它们并重新键入它们以删除零宽度空格字符。

FWIW:Dreamweaver 的“显示隐藏字符”没有显示这些字符。虽然考虑了一下,但不确定它如何显示零宽度的内容,但如果有某种针对此类字符的指示符,那就太好了。我将在以下位置记录错误:http://adobe.ly/DWwish 请也这样做,越多记录它的人,它被实施的机会就越大。

When I copy and pasted your code above and tired it within Dreamweaver, as well as in IE9 and FIrefox and all errored out. I checked in the console for IE9 and Firefox and both indicated an illegal character after the semi-colon at the end of the last line (the document.write line). In checking the source for the code above, I see that there is a string of 10 "​" characters ( zero-width space ) after that last semi-colon. Not sure how they got there, but once I removed them from my code the page began working in IE9, Firefox and Dreamweaver's Live View.

To do this I basically had to select the trailing semi-colon and the opening < character, delete them and re-type them in order to delete the zero width space characters.

FWIW: Dreamweaver's Show Hidden Characters didn't show these characters. Although thinking about it, not sure how it would show something that's zero width anyway, but it would be good if there were some sort of indicator for such characters. I'll log a bug at: http://adobe.ly/DWwish please do so as well, the more people that log it the more chance it'll get implemented.

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