我使用下面的代码替换
$('iframe').map(function(i, el) {
var amp_iframe = $('<amp-iframe layout="responsive"></amp-iframe>');
amp_iframe.attr('width', $(this).attr('width'));
amp_iframe.attr('sandbox', 'allow-scripts allow-same-origin');
return $(this).replaceWith(amp_iframe);
});输出如下:
<amp-iframe layout=responsive src="https://docs.google.com/forms/d/e/abcxxxxxx/viewform?embedded=true" width=100% height=750 frameborder=0 sandbox="allow-scripts allow-same-origin"></amp-iframe>我希望将width="100%"转换为width="100"并删除%。我该怎么做?
发布于 2018-02-20 11:45:47
如果您从"100%"获得$(this).attr('width'),那么您可以使用:
$(this).attr('width').replace(/\%/, "")但是,要确保通过添加故障安全代码来获得$(this).attr('width')的价值,否则它就会崩溃。
编辑:是的。您可以这样替换:
let width = $(this).attr('width').replace(/\%/, "")
amp_iframe.attr('width', width); https://stackoverflow.com/questions/48884438
复制相似问题