更新:如果需要,将使用jquery ... 好吧,我几乎所有工作都需要......我只是有一个简单的问题。如何将其他html / javascript添加到document.write函数中?我正在尝试使用window.open(是的,我知道我应该调用页面,但我想只使用一个页面 - index.html这样做)我希望嵌入一些图像和另一个按钮但它赢了干嘛?我需要做些什么才能让它发挥作用? (窗口2上按钮的javascript应该去哪里?)
背景故事: index.html有1个链接和1个按钮。按钮打开窗口1,链接打开窗口2.窗口2有一个按钮,可以关闭窗口1.结束!
index.html:
<head>
<script type="text/javascript">
function openWin1()
{
myWindow=window.open('','','');
myWindow.document.write("<p>I want flower.jpg here</p>");
myWindow.focus();
}
function openWin2()
{
myWindow=window.open('','','');
myWindow.document.write("<p>I want bee.jpg here, a background color and a button
that will close window1</p>");
myWindow.focus();
}
</script>
</head>
<body>
<input type="button" value="Window 1" onclick="openWin1()" /><br />
<a href="javascript:openWin2()">Window 2</a>
</body>
</html>
答案 0 :(得分:0)
function openWin1()
{
myWindow=window.open('','','');
myWindow.document.write("<p><img src=\"http://www.digital-photography-school.com/wp-content/uploads/2007/11/flower.jpg\" /></p>");
myWindow.focus();
}
function openWin2()
{
myWindow=window.open('','','');
myWindow.document.write("<style type=\"text/css\">body{background-color:yellow;}</style><p><img src=\"http://4.bp.blogspot.com/_YGkSujzLG1g/S8eFOjiQ7yI/AAAAAAAAAA8/wSRi2dGDr-c/s400/bee.jpg\" /></p><input type=\"button\" onclick=\"window.close();\" value=\"close me\" />");
myWindow.focus();
}
答案 1 :(得分:0)
阅读完评论后,我认为您的问题实际上归结为需要知道如何在字符串中转义括号。如果您执行以下操作,则会出现语法错误:
myWindow.document.write("<p>I want <img src="myimageurl">flower.jpg here</p>");
是"
结束你的字符串。您可以使用\
字符来转义字符串,如下所示:
myWindow.document.write("<p>I want <img src=\"myimageurl\">flower.jpg here</p>");
这不会给你语法错误。
答案 2 :(得分:-1)
试试这个
<head>
<script type="text/javascript">
var Window1, Window2;
function openWin1()
{
Window1=window.open('','','');
Window1.document.write("<p>I want flower.jpg here</p>");
Window1.focus();
}
function openWin2()
{
Window2=window.open('','','');
Window2.document.write("<p>I want bee.jpg here, a background color and a button
that will close window1</p>");
Window2.focus();
}
</script>
</head>
<body>
<input type="button" value="Window 1" onclick="openWin1()" /><br />
<a href="javascript:openWin2()">Window 2</a>
</body>
</html>
To close Window1 from a button in Window2 use the below code
function closeWindow2(){
window.opener.Window1.close();
}
This button will be in Window2
<input type="button" onclick="closeWindow2()" />