我正在尝试使用下面的href链接实现两件事。首先,我想启动一个弹出窗口。完成。接下来,我希望弹出窗口显示iframe。这很容易实现直到我意识到我需要将href链接文本作为iframe src中的参数传递。
例如,除非src="http://localhost:8080/test/document.html?OnSale"
我无法弄清楚为什么我的html页面正文中的document.write
将不会打印动态iframe我试图在href链接中使用我的foo()函数创建...
<div id="blanket" style="display:none;"></div>
<div id="popUpDiv" style="display:none;">
<a href="#" onclick="popup('popUpDiv')">
<img align="right" src="http://localhost:8080/test/img/close_img.png">
</a>
<script type="text/javascript">
function foo(obj)
{
test1 = "http://localhost:8080/test/document.html?"+obj.text;
document.write('<iframe height="450" allowTransparency="true" frameborder="0" scrolling="yes" style="width:100%;" src="'+test1+'" type= "text/javascript"></iframe>');
}
</div>
<a href="#" onclick="popup('popUpDiv');foo(this);">OnSale</a>
修改 这是我的完整HTML页面。一切都在tomcat7 w / win7和firefox上本地运行。
<html>
<head>
<script type="text/javascript" src="http://localhost:8080/test/css-popup/css-pop.js"></script>
<link href="http://localhost:8080/test/css-popup/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="blanket" style="display:none;"></div>
<div id="popUpDiv" style="display:none;">
<a href="#" onclick="popup('popUpDiv')">
<img align="right" src="http://localhost:8080/test/css-popup/x.png">
</a>
<script type="text/javascript">
function foo(obj){
test1 = "http://localhost:8080/test/document.html?"+obj.innerHTML;
document.write('<iframe height="450" allowTransparency="true" frameborder="0" scrolling="yes" style="width:100%;" src="'+test1+'" type= "text/javascript"></iframe>');
}
</script>
</div>
<a href="#" onclick="popup('popUpDiv');foo(this);">OnSale</a>
</body>
</html>
答案 0 :(得分:2)
text
不是所有浏览器的标准,请尝试innerHTML
而不是
function foo(obj){
test1 = "http://localhost:8080/test/document.html?"+obj.innerHTML;
document.write('<iframe height="450" allowTransparency="true" frameborder="0" scrolling="yes" style="width:100%;" src="'+test1+'" type= "text/javascript"></iframe>');
}
在您分享完整个代码后更新
据我了解,您想要打开一个弹出窗口,并在其中显示动态创建的iframe。但document.write适用于您当前的窗口。所以你必须先处理弹出窗口。然后改变那个内容。
试试这个,
<html>
<head>
<script type="text/javascript" src="http://localhost:8080/test/css-popup/css-pop.js"></script>
<link href="http://localhost:8080/test/css-popup/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="blanket" style="display:none;"></div>
<div id="popUpDiv" style="display:none;">
<a href="#" onclick="popup('popUpDiv')">
<img align="right" src="http://localhost:8080/test/css-popup/x.png">
</a>
<script type="text/javascript">
var popUpWindow;
function popup(n) {
popUpWindow = window.open(n);
}
function foo(obj){
test1 = "http://localhost:8080/test/document.html?"+obj.innerHTML;
popUpWindow.document.write('<iframe height="450" allowTransparency="true" frameborder="0" scrolling="yes" style="width:100%;" src="'+test1+'" type= "text/javascript"></iframe>');
}
</script>
</div>
<a href="#" onclick="popup('popUpDiv');foo(this);">OnSale</a>
</body>
</html>