使用jquery在body中创建iframe

时间:2013-01-14 16:39:08

标签: javascript jquery html

我面临一些问题。我想在div标签内创建一个iframe,它将在我的body标签内。我尝试了以下jQuery代码:

$(document.createElement('<div id="modalDiv"><iframe id="modalIFrame" width="100%" height="100%" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" title="Dialog Title"></iframe></div>'));

但我没有得到任何结果。此外,我将脚本放在body标签中,但没有得到任何结果。

提前致谢。

3 个答案:

答案 0 :(得分:3)

假设您添加了jQuery:

$(document).ready(function(){
    $('body').append('<div id="modalDiv"><iframe id="modalIFrame" width="100%" height="100%" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" title="Dialog Title"></iframe></div>');
});

这会将你的代码添加到正文html的末尾......

答案 1 :(得分:0)

createElement的参数必须是包含元素类型名称的字符串。您正在提供HTML片段。

由于您使用的是jQuery,只需从该字符串构造一个jQuery对象。

jQuery('<div id="modalDiv"><iframe id="modalIFrame" width="100%" height="100%" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" title="Dialog Title"></iframe></div>');

答案 2 :(得分:0)

document.createElement()不支持从HTML字符串创建元素,但您可以将字符串传递给jQuery以构建内容,即:

var newContent = $('<div id="modalDiv"><iframe id="modalIFrame" width="100%" height="100%" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" title="Dialog Title"></iframe></div>');
newContent.appendTo("body");

请务必将选择器更新为适合您的选择,这里只是添加到body元素。

此外,如果iframe实际上指向资源,请务必同时设置src属性。

希望有所帮助!欢呼声。