使用 Javascript 更改 iframe src
当有人单击单选按钮时,我试图更改 。由于某种原因,我的代码无法正常工作,我无法找出原因。这是我所拥有的:
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<script>
function go(loc) {
document.getElementById('calendar').src = loc;
}
</script>
</head>
<body>
<iframe id="calendar" src="about:blank" width="1000" height="450" frameborder="0" scrolling="no"></iframe>
<form method="post">
<input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Day
<input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Week
<input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Month
</form>
</body>
</html>
I am trying to change an <iframe src=... >
when someone clicks a radio button. For some reason my code is not working correctly and I am having trouble figuring out why. Here is what I have:
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<script>
function go(loc) {
document.getElementById('calendar').src = loc;
}
</script>
</head>
<body>
<iframe id="calendar" src="about:blank" width="1000" height="450" frameborder="0" scrolling="no"></iframe>
<form method="post">
<input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Day
<input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Week
<input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Month
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在这种情况下,可能是因为您在这里使用了错误的括号:
应该是
In this case, it's probably because you are using the wrong brackets here:
should be
也许这会有所帮助...它是纯 html - 没有 javascript:
顺便说一句,有些网站不允许您在 iframe 中打开它们(安全原因 - 点击劫持)
Maybe this can be helpful... It's plain html - no javascript:
By the way some sites do not allow you to open them in iframe (security reasons - clickjacking)
这是 jQuery 的方法:
Here's the jQuery way to do it:
onselect
必须是onclick
。这适用于键盘用户。我还建议在“日”、“月”和“年”的文本中添加
标签,以便更容易点击。示例代码:
我还建议删除属性
onclick
和值之间的空格,尽管它可以被浏览器解析:应该是:
The
onselect
must beonclick
. This will work for keyboard users.I would also recommend adding
<label>
tags to the text of "Day", "Month", and "Year" to make them easier to click on. Example code:I would also recommend removing the spaces between the attribute
onclick
and the value, although it can be parsed by browsers:Should be:
这也应该有效,尽管
src
将保持不变:This should also work, although the
src
will remain intact:这是我更新的代码。它工作得很好并且可以帮助你。
Here is my updated code. It works fine and it can help you.
其他答案都不适合我,更改 src 后 iframe 没有刷新,所以这就是最终的结果:
MDN
None of the other answers worked for me, the iframe wasn't refreshing after changing the
src
so this is what finally did:MDN
将输入中的
onselect
更改为onchange
并使用change
onselect
toonchange
in inputs and use如何使用Javascript函数更改iframe src?
HTML:
JS:
How to change iframe src with Javascript function?
HTML:
JS:
这很简单。您可以更改元素的 src 属性。请参阅下面的代码以供参考。
注意:上面代码中的“framebox”是iframe的id
It's very simple. You can change the src property of the element. See the below code for reference.
note : "framebox" is the id of iframe in the above code
你可以通过在javascript中制作iframe来解决它
You can solve it by making the iframe in javascript