简体   繁体   English

如何使用jquery动态创建对象标签并向其中添加替代内容?

[英]how do I dynamically create an object tag with jquery and add alternate content to it?

I am trying to embed a pdf into my webpage using an object that will fallback to an iframe which loads the pdf through an external rendering service for users that do not have acrobat installed. 我正在尝试使用一个对象将pdf嵌入到我的网页中,该对象将回退到iframe,该iframe通过外部渲染服务为未安装acrobat的用户加载pdf。

I am trying to do it like this: 我正在尝试这样做:

var container = document.createElement("div");
var object = document.createElement("object");
var iframe = document.createElement("iframe");

$(container).append(object);
$(object).append(iframe);
$("body").append(container);

This works in firefox but causes an error in IE in the jquery core code. 这在Firefox中有效,但在jQuery核心代码中的IE中导致错误。 It breaks when I append the iframe to the object. 当我将iframe附加到对象时,它会中断。

I need to create the content in a way that will give me access to both the object and the iframe element because I do not have a reliable way to detect if the user has acrobat or not, so I am depending on the browser to display the correct content and just styling both the iframe and the object so that either one will look okay. 我需要以一种可以使我同时访问对象和iframe元素的方式来创建内容,因为我没有可靠的方法来检测用户是否具有acrobat,因此我依靠浏览器来显示正确的内容,并仅对iframe和对象进行样式设置,以便两者看起来都可以。

What is an alternate and cross-browser approach? 什么是跨浏览器的替代方法?

have you tried it like this? 你有这样尝试过吗?

var object = $("<object>");
var container = $("<div>").append(object);
$(object).append("<iframe>");
$("body").append(container);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM