如何使用jQuery将iframe添加到div中

3

我在这里找到了接受答案中的代码... 该代码通过按钮点击将一个iframe加载到特定区域...

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

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

我该如何更改这个代码,以便允许我设置高度、宽度和滚动条的样式呢?

2个回答

2
theIframe.style.*

需要样式化的任何属性。


2
这不是jQuery,而是纯Javascript代码:
function setIframe(element,location){
    var theIframe = document.createElement("iframe");
    theIframe.src = location;
    theIframe.setAttribute("weight", "500");
    theIframe.setAttribute("width", "500");
    theIframe.setAttribute("scrolling", "yes");
    element.appendChild(theIframe);
}

这不是你的问题的一部分,但以下是使用jQuery切换div的方法:
标记:
```html
内容
```
脚本:
```javascript $('.toggle').click(function(){ $(this).toggleClass('active'); }); ```
<input type="button" value="Toggle Div"  id="btnToggle"/>

    <div id="randomContent">
        This is random Content.
        <br />
        This is random Content.
        <br />
        This is random Content.
        <br />
        This is random Content.
        <br />
        This is random Content.
        <br />
        This is random Content.
        <br />
    </div>

jQuery:

    $(document).ready(function () {
        $("#btnToggle").bind("click", function () {
            $("#randomContent").toggle();
        });
    });

如果我不想或者“不应该”将其放入iframe中,但是我希望基于两个不同的按钮切换隐藏和显示div,我在stackoverflow上提出了一个单独的问题...很快会发布链接。 - Sun Rhythms
谢谢你...这是链接。有两个按钮。一个用于提交表单,另一个用于在点击按钮后显示表单。http://stackoverflow.com/questions/14651706/how-to-hide-form-on-submit-and-reveal-it-on-a-button-click - Sun Rhythms

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接