【问题标题】:How to create an iframe on clicking a link?如何在点击链接时创建 iframe?
【发布时间】:2012-11-27 12:27:56
【问题描述】:

我有一个网页,表格中有几个链接。其中一个链接位于 td 标签内。我想要一种调用 iframe 的方法,一旦用户点击链接就会打开它。当 iframe 弹出时,页面的其余部分将变得无响应,一旦用户离开 iframe,外部页面就会变得响应。表 td 是这样的:

<td >
<a href="configuration.xml related jsp action">Set the Iframe up</a>
</td>

iframe 将由另一个 jsp 的内容填充。

编辑:我很着急,所以忘记粘贴我尝试过的代码:所以这里是:

<td >
<a href="#">Set the Iframe up</a>
<div id="modalMan" style="display:none;">
<div class="modalsub" >
<p class="close"><a href="#"><img src="cbutton.gif" alt="Close"/></a></p>
<iframe id="myFrame" style="display:none;" height="430" width="675" 
src="myJSP.jsp" >
</iframe>
</div>
</div>
</td>

然而,这会产生一个问题,因为关闭图标没有出现并且 div 的一部分,即

   <div id ="modalman">
   <p class>
   <iframe src>

所有内容都保持隐藏,并以类似的外观打开 iframe,但功能都被打乱了,myjsp 的 javascript 内容保持隐藏。

再次编辑:我很抱歉我忘了提到我已经尝试过 onclick 功能,但是由于我的 href 是这样的,所以它会直接在新的浏览器选项卡中打开 jsp在 iframe 中打开它。

 <a href="configuration.xml related jsp action">Set the Iframe up</a>

我怎样才能做到这一点?请提出一种方法。

【问题讨论】:

  • 感谢大家的热烈响应。我正在一一检查。

标签: javascript jsp


【解决方案1】:

这是一个非常基本的 JavaScript 函数,可以做到这一点。函数参数是您希望 iFrame 附加到的元素以及您希望它指向的位置。

HTML

<a href="#" onclick="setIframe(this,'http://www.stackoverflow.co.uk')" >Set the Iframe up</a>

JavaScript

function setIframe(element,location){
    var theIframe = document.createElement("iframe");
    theIframe.src = location;
   element.appendChild(theIframe);
}

【讨论】:

  • 很抱歉我忘了提我已经尝试过onclick功能,但是由于我的href是这样的设置iframe a> 所以它会直接在新的浏览器选项卡中打开 jsp,而不是在 iframe 中打开它。
  • 我不太明白你是说你希望 iFrame 位置是&lt;a&gt;href
  • 不,我有在 href 中出现的 xml 相关操作,这是 java 处理所需要的。因此,当我执行 onclick 时,iframe 不会在弹出窗口中打开,而是会在新的浏览器窗口中打开 jsp。
  • 有没有办法在不使用 href="#" 的情况下做到这一点?因为这会导致浏览器滚动到页面顶部
  • 您可以阻止默认行为,也可以使用另一个“空 href”占位符。见this question
【解决方案2】:

如果您使用jquery(并且应该),您可以在链接的onlick 中运行以下操作:

$('#foobar').append('<iframe></iframe>')

其中 foobar 是 iframe 的父元素的 ID。

【讨论】:

    【解决方案3】:

    你也可以尝试使用带有fancybox插件的jQuery

    在此处尝试示例代码,http://jsfiddle.net/muthkum/ssWMc/1/

    这是完整的代码,

    <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.js'></script>
    <script type='text/javascript' src="http://fancybox.net/js/fancybox-1.3.4/jquery.fancybox-1.3.4.js"></script>
    <link rel="stylesheet" type="text/css" href="http://fancybox.net/js/fancybox-1.3.4/jquery.fancybox-1.3.4.css">
    
    <script>
    $(function(){
        $("a").fancybox();
    })
    </script>
    
    <a class="various iframe" href="http://www.youtube.com/embed/L9szn1QQfas?autoplay=1">Youtube (iframe)</a>​
    

    【讨论】:

    • 不能使用fancybox。我的组织代理不允许这样做。
    • 你也可以从fancybox.net下载fancybox并将它放到你的服务器上。无论如何,欢迎您。
    【解决方案4】:

    您听说过colorboxiframe:true 属性吗??

     <p><a class='iframe' href="http://google.com">Outside Webpage (Iframe)</a></p>
    

    示例代码From

     $(document).ready(function(){
                //Examples of how to assign the ColorBox event to elements
                $(".iframe").colorbox({iframe:true, width:"80%", height:"80%"});
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-28
      • 2014-03-04
      • 2018-12-12
      • 1970-01-01
      • 2020-06-28
      • 2012-01-24
      • 1970-01-01
      • 2016-07-31
      相关资源
      最近更新 更多