javascript - 如何从 iframe 标记中删除样式(不是在 iframe 内,而是在标记内)

标签 javascript jquery iframe

我的服务器上的页面有多个具有不同 ID 的 iframe 标记。我添加了一个 iframe 标签来嵌入 YouTube 视频。 iframe 标签有一个样式元素,它具有 250 宽度和 250 高度,由插件添加。我想删除添加到 iframe 的此样式,以便 YouTube 视频仅显示此特定 iframe 中 iframe 标记的高度和宽度,因为页面上还有其他 iframe 标记具有不应触及的样式。

1)我添加的代码(注意宽度=640高度=390)

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/GxTl1Ykbuww?enablejsapi=1&origin=origin-domain.com" frameborder="0"></iframe>

2)用浏览器加载时页面上的代码(注意 style="height:250;width:250;" 现在已添加到 iframe 标记中,这是我想要的垃圾要删除,但只能从此 iframe 标记 id 中删除

<iframe id="player" style="height: 250px; width: 250px;" src="http://www.youtube.com/embed/GxTl1Ykbuww?enablejsapi=1&amp;origin=origin-domain.com" height="390" width="640" frameborder="0"></iframe>

Javascript 或 Jquery 会很好。但是 jquery 中的删除标记会从页面上的每个 iframe 中删除样式。

理想的解决方案将导致只有具有 id="player" 的 iframe 从上面的 2) 更改为不再具有 style="" 或具有style=""但里面什么也没有。

最佳答案

$('#player').removeAttr('style')

应该使用最新的 jQuery 工作

作为解释

$('#player')

以 id='player' 为目标 DOM 元素(# 以 id 为目标,如 css 选择器)。

removeAttr('style')

从目标对象中删除“style”属性

关于javascript - 如何从 iframe 标记中删除样式(不是在 iframe 内,而是在标记内),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16293984/

相关文章:

javascript - 在 Dart 中编写 WebWorker(没有 dart :isolate)

javascript - 在javascript对象选择器中使用变量

javascript - 如果导入 events-polyfill,React Datepicker 将无法正常工作

javascript - 隐藏 jQuery dataTable 中的列

jquery - 德鲁帕尔 6 : best way to upgrade jQuery?

javascript - 从父页面更改 iframe 中 div 的背景颜色不起作用

javascript - 了解 Backbone.js 事件处理程序

javascript - contenteditable - 选择 2 个子段落并覆盖文本(Firefox 问题)

javascript - 在点击时播放iframe视频(带有弹出窗口),并在弹出窗口关闭时暂停

html - 如何扩大我的 iframe 以保持纵横比?