javascript - 从 javascript 或 jquery 中的 htmlstring 查找宽度和高度

标签 javascript jquery

我需要搜索包含一些 html 的 iframe 字符串。我需要找到宽度和高度。

字符串示例:

var stringEx = '<iframe width="560" height="315" src="//www.youtube.com/embed/spr0ZHC2x2o" frameborder="0" allowfullscreen></iframe>'

最佳答案

您可以通过执行以下操作将其设为 jQuery 对象:

var $iframe = $('<iframe width="560" height="315" src="//www.youtube.com/embed/spr0ZHC2x2o" frameborder="0" allowfullscreen></iframe>');

然后你可以使用.attr()来获取widthheight属性:

var width = $iframe.attr('width'),
    height = $iframe.attr('height');

演示:http://jsbin.com/kuqajelo/1/edit?html,js,output

关于javascript - 从 javascript 或 jquery 中的 htmlstring 查找宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24090060/

相关文章:

javascript - 从 Angular.js 中的 View 修改方法中的数据的最佳实践

javascript - 有没有办法在每个单个文件中定义这些 react 类

javascript - jQuery/Javascript - 如果当前 url 包含/foo/,则显示/隐藏 ID

javascript - 本地脚本/css 与移动 PhoneGap 应用程序上的 CDN

javascript - 使用异步中断 Node 脚本

javascript - css 打印预览页面被剪切

javascript - 为什么 AJAX GET 请求只能工作一次?

javascript - 为什么我们在揭示模块模式中使用自执行函数?

javascript - Angular ui-sortable 不适用于移动设备

javascript - 多维php数组到js变量