在同一标签中显示图像或 swf
我的网页通过 AJAX 调用从 XML 获取内容。我从该 XML 中检索 URL 并使用它在页面上创建一个元素。
但该 URL 可能类似于:
http://something/something.jpg
- 或
http://something/something.swf
这意味着它可以是图像文件或可以是 .swf 文件,因此我无法在同一标记(也是动态创建的)中显示该图像或 .swf 文件。
我不知道如何在同一标签中显示图像和 .swf 文件。我该如何解决这个问题?
My webpage gets the content from XML via an AJAX call. I retrieve a URL from that XML and use it to create an element on the page.
But that URL can look like:
http://something/something.jpg
,- or
http://something/something.swf
This means it can be a image file or can be a .swf file, so I am unable to display that image or .swf file in same tag (which is also created dynamically).
I don't know how to display both image and .swf file in same tag. How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不在创建标签之前检查 URL?您可以使用正则表达式,如下所示:
i 使其无论大小写都匹配(因此它也会匹配“something.SWF”),并且
toLowerCase
确保我们可以将其与小写字符串进行比较(例如if (类型=== 'swf') { }
)。现在您可以使用结果创建正确的标签,如下所示:
请记住,此示例未经测试。例如,您可能需要设置
标记上的
height
和width
属性。注意:
match
返回一个数组其中包含整个比赛以及任何匹配的组。在这种情况下,数组将如下所示:['jpg', 'jpg']
。这是因为(jpg|swf)
组以及完整匹配项均已保存。如果您需要防止这种行为,您可以使用非捕获组,如下所示:(?:jpg|swf)
,这样该组就不会被记住。Why don't you check the URL before creating the tag? You could use a regular expression, like so:
The
i
in the regular expression makes it match regardless of case (so it will match "something.SWF" as well) andtoLowerCase
makes sure we can compare it to a lowercase string (e.g.if (type === 'swf') { }
).Now you can use the result to create the correct tag, like this:
Please keep in mind that this example is untested. For example, you probably need to set the
height
andwidth
attributed on the<embed>
tag.NB:
match
returns an array which contains the entire match, plus any matched groups. In this case, the array will look like this:['jpg', 'jpg']
. That's because the group,(jpg|swf)
, which is saved as well as the complete match. If you need to prevent this behaviour, you can use a non-capturing group, like this:(?:jpg|swf)
, so the group won't be remembered.