好的,我有一个很大的问题,我已经找了好几天关于如何做到这一点。要么我读得不够好,无法理解它,要么我很愚蠢。我还不确定它是什么。老实说,这是家庭作业,但我已经苦苦挣扎了3天了,因为这是一个在线课程,我不能去找我的老师,问他我做错了什么。我已经给他发了电子邮件,但他的帮助是有限的和模糊的,我无法弄清楚这一点。不管怎么说,说到点子上。我想使用JavaScript函数将HTML添加到要在新窗口中显示的文本中。这是我所拥有的基础知识。
function myWindow(){
var pageContent, pageTitle;
pageTitle = document.write("Selected Image Preview");
document.write.style.textAlign="center";
pageContent = "<html><head><title>";
pageContent += pageTitle + "</title>";
pageContent += "<script>alert('The page ' + document.title + ' was created: ' + document.lastModified)</script>";
pageContent += "<h3>"Name of Image"</h3>";
pageContent += "</head><body align="center"><strong>" + "<font color= " violet ">"Here is the image you selected. "</font>";
pageContent += "</strong></body></html>";
}现在,我不知道我做错了什么,考虑到我已经阅读了几乎所有我能找到的东西,在这个网站上搜索,以及其他几十个网站。我尝试过W3学校,一些网站看起来像是在2001年最后一次更新,我的书绝对没有在函数中使用超文本标记语言的例子(它是一本javascript书,所以超文本标记语言的帮助非常有限)。从顶部开始,它告诉我脚本行上的"Uncaught :Uncaught非法junk.html:16“。那么它就不会加载页面的其余部分。如果我注释掉它,它会告诉我'<h3>‘是一个意外的标识符,并且它一直在运行。总会有一些错误,如果我注释掉那些给出错误的行,那么就什么都没有了。请帮我找出我做错了什么。如果有必要,我会使用<body onload="myWindow();">标记调用函数onload。
附言:如果我的格式不正确,请不要杀我。我确实阅读了说明,并尝试尽可能整洁地格式化。
发布于 2014-09-20 12:07:32
最大的问题是,调用alert()的行中的结束</script>标记终止了脚本,即使它在字符串文字内。请参阅我评论中的链接,指向您的原始帖子。引用还有其他一些问题,如果一位老师真的在2014年教<font>标签,我想我应该找到他,吐在他的腿上。
请注意,</script>中的斜杠和嵌入的双引号现在使用反斜杠进行转义。这是最大的变化。此外,函数现在返回计算值,以便可以使用它。
这段代码经过了JavaScript控制台的清理。它不会打开任何新窗口,也不会处理我无法理解的"style“行。
function myWindow(){
var pageContent, pageTitle;
pageTitle = "Selected Image Preview";
// document.write.style.textAlign="center"; // WTF?
pageContent = "<html><head><title>";
pageContent += pageTitle + "</title>";
pageContent += "<script>alert('The page ' + document.title + ' was created: ' + document.lastModified)<\/script>";
pageContent += "</head>";
pageContent += "<body style=\"text-align: center;\">";
pageContent += "<h3>Name of Image</h3>";
pageContent += "<strong>" + "<font color= \" violet \">\"Here is the image you selected. \"</font>";
pageContent += "</strong></body></html>";
return(pageContent);
}我已经编辑了代码。样式行在文档的头部,现在是固定的,我根据您关于希望文本居中的评论向<body>添加了一个<h3>属性。
发布于 2014-09-20 11:37:05
pageContent += "<h3>"Name of Image"</h3>";你不需要用引号把镜像的名字括起来。整行应该被视为一个字符串。
pageContent += "<h3>Name of Image</h3>";基本上,HTML标记中的任何内容都不需要引号,除非您打算显示引号。
对于此行:
pageContent += "</head><body align="center"><strong>" + "<font color= " violet ">"Here is the image you selected. "</font>";您应该使用单引号。
pageContent += "</head><body align='center'><strong>" + "<font color='violet'>Here is the image you selected. </font>";你应该能够修复你的HTML的其余部分,记住属性的单引号,没有内容的引号。
至于HTML本身,它看起来应该至少遵循预期的标准。最终,您应该将大多数样式转移到CSS。
<html>
<head>
<title>Selected Image Preview</title>
<script>// your script here </script>
</head>
<body>
<div align='center'>
<!-- your content here -->
</div>
</body>发布于 2014-09-20 11:40:45
好的,您的代码包含错误,因为您需要学习如何使用字符串和引号以及如何转义引号。
var str1 = "qwe";
var str2 = "asd";
var str3 = str1 + str2; // will be qweasd
var str3 = str1 + '1111' + str2; // will be qwe1111asd
var str3 = str1 + 'z"1"' + str2; // will be qwez"1"asd
var str3 = str1 + "z\"1\"" + str2; // will be qwez"1"asd. There is no difference if you use double quotes or single. If you use single quotes, all single quotes in the string must be escaped with backslash and opposite with double quotes
// and the same with single quotes:
var str3 = str1 + 'z\'1\'' + str2; // will be qwez'1'asd 另外,您使用的是document.write函数,它覆盖了当前页面的内容,但是您需要一个新窗口,这就是为什么我们应该使用函数window.open,它返回一个新的窗口处理程序。我们将其保存到OpenWindow变量中,然后使用OpenWindow.document.write应用我们的内容,将字符串pageContent作为第一个参数传递
和正确的代码:
function myWindow(){
var pageContent, pageTitle;
document.title = "Selected Image Preview";
document.body.style.textAlign="center";
pageContent = "<html><head><title>";
pageContent += pageTitle + "</title>";
pageContent += "<script>alert('The page ' + document.title + ' was created: ' + document.lastModified)</script>";
pageContent += "<h3>Name of Image</h3>";
pageContent += '</head><body align="center"><strong><font color="violet">Here is the image you selected.</font>';
pageContent += "</strong></body></html>";
var OpenWindow = window.open('#','_blank','width=335,height=330,resizable=1');
OpenWindow.document.write(pageContent);
}https://stackoverflow.com/questions/25944917
复制相似问题