[英]change the src of an iframe using javascript
<table>
<tr>
<td>one</td>
<td>two</td>
</tr>
<tr>
<td>three</td>
<td>four</td>
</tr>
</table>
<div id="numbers">
<iframe name="letters" src="">
</iframe>
</div>
I'd like to have the ability to click on any cell in the table, grab the value inside, show the div
and append the iframe src
based on the cell's value. 我希望能够单击表中的任何单元格,获取其中的值,显示div
并根据该单元格的值附加iframe src
。 When the user clicks on the cell again the iframe
becomes blank and the div
is hidden. 当用户再次单击该单元格时, iframe
变为空白,并且div
被隐藏。
For example: click cell one
, show the div
with the iframe src="/one.html"
. 例如:单击单元格one
,用iframe src="/one.html"
显示div
。 Click it again, hide the div with a blank iframe
. 再次单击它,用空白iframe
隐藏div。
Thanks in advance! 提前致谢!
(Please, no jQuery answers.) (请,没有jQuery答案。)
Without adding an id to the iframe
use this: 无需在iframe
添加ID,请使用以下代码:
document.getElementsByTagName("iframe")[0].src="http://example.com/";
Or based on the name: 或根据名称:
document.getElementsByName("letters")[0].src="http://example.com/";
If you would add a id: 如果您要添加ID:
<iframe name="letters" id="letterframe" src="" />
document.getElementById("letterframe").src="http://example.com/";
Added ids to some of the HTML elements. 为某些HTML元素添加了ID。
<table id="table">
<tr>
<td>one</td>
<td>two</td>
</tr>
<tr>
<td>three</td>
<td>four</td>
</tr>
</table>
<div id="numbers">
<iframe id="iframe" name="letters" src="">
</iframe>
</div>
And the javascript 和JavaScript
document.getElementById('table').onclick=function(e){
e = e || window.event;
e=e.target || e.srcElement;
if(e.tagName.toLowerCase()=='td'){
var page = e.innerHTML,iframe=document.getElementById('iframe');
if(iframe.src.indexOf(page)>-1){
document.getElementById('numbers').style.display='none';
}
else{
document.getElementById('numbers').style.display='block';
iframe.src='/'+page+'.html';
}
}
}
It should be something like below.Call this function on onclick
event of td
and pass desired src
in parameter. 应该是下面这样:在td
onclick
事件上调用此函数,并在参数中传递所需的src
。
<script type=”text/javascript”>
function changeURL(srcvalue)
{
document.getElementById('letters').src=srcvalue;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.