通过 iFrame 进行链接
我遇到问题,我尝试单击链接,但无法使用目标:iFrame 的名称打开链接。我不想使用 href 因为我要显示/隐藏 div。
JavaScript:
<script type="text/javascript">
<!--//
function godirect(url, targetname)
{
document.getElementById(targetname).src = url;
//frame[targetname].location.href = url;
}
//-->
</script>
HTML 和 PHP:
$a=0;
echo '<a href="#" onclick="godirect("http://www.google.com", iframe_url'.$a.');">Click Me!</a>';
echo '<iframe class="iframe_url" id="iframe_url'.$a.'"></iframe>';
I have problem and I tried click link then it doesn't work to open link using target: name of iFrame. i dont want use href because im going make show/hide div.
Javascript:
<script type="text/javascript">
<!--//
function godirect(url, targetname)
{
document.getElementById(targetname).src = url;
//frame[targetname].location.href = url;
}
//-->
</script>
in HTML and PHP:
$a=0;
echo '<a href="#" onclick="godirect("http://www.google.com", iframe_url'.$a.');">Click Me!</a>';
echo '<iframe class="iframe_url" id="iframe_url'.$a.'"></iframe>';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样
How about
您必须在 JavaScript 中引用字符串。您正在尝试通过传入尚未定义的变量来获取元素的 id。
您还可以使用与分隔 JS 字符串相同的引号字符来分隔 HTML 属性值。
要使用您正在使用的方法,同时进行最少数量的修复以使其工作:
不过,使用 JS 首先是一个非常愚蠢的想法,并且当 JS 不可用时,您的实现无法有任何类型的后备可用(这很奇怪,因为您正在采取措施阻止无法识别 将 JS 渲染为内容文本)。
您可以使用纯 HTML 来执行此操作:
您可以做到这一点,也可以拥有正常的、功能正常的链接。 建立在有效的基础上。
You have to quote strings in JavaScript. You are trying to get the id of the element by passing in a variable which you haven't defined.
You are also using the same quote characters to delimit your HTML attribute value as you are using to delimit your JS strings.
To use the approach you are using, while making the minimum number of fixes to make it work:
Using JS for this is a very silly idea in the first place though, and your implementation fails to have any kind of fallback for when JS is not available (which is odd, since you are taking steps to stop browsers which don't recognise the script element from rendering the JS as content text).
You can do this with plain HTML:
You can do that as well as having a normal, functioning link. Build on things that work.
试试这个:
Try this: