[英]How to add iFrame attributes, that will not validate as HTML5, via JQuery
I have some iFrames on an HTML5 page, each with the class "a-iframe". 我在HTML5页面上有一些iFrame,每个iFrame都带有“ a-iframe”类。 There are about 4 of these frames, all of different shapes and sizes, on the page.
页面上大约有四个这样的框架,它们的形状和大小都不同。
How do I get each of these frames, by class name and apply these attributes, that will not validate in HTML by W3C validator: 我如何通过类名获取这些框架中的每一个并应用这些属性,这些属性将无法通过W3C验证程序在HTML中进行验证:
scrolling="no" frameborder="0" allowtransparency="true"
My current code, that does not work right, is below: 我当前无法正常工作的代码如下:
<script type="text/javascript">
$.each($(".a-iframe"), function()
{
$(this).attr({
scrolling: "no",
frameborder: "0",
allowtransparency: "true"
});
});
</script>
This should do the trick. 这应该可以解决问题。
$(document).ready(function() { $(".a-iframe").each(function() { $(this).attr({ scrolling: "no", frameborder: "0", allowtransparency: "true" }); console.log($(this)); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <iframe src="#" class="a-iframe" scrolling="no" frameborder="0" allowtransparency="true"></iframe>
After I play your codes for awhile, I observed on jsfiddle that maybe it's because whether the scrollbar is showed or not is determined when content load-in, not when attribute is set, so I altered to this: 在播放了一段时间的代码之后,我在jsfiddle上观察到,也许是因为滚动条是否显示是在加载内容时决定的,而不是在设置属性时决定的,所以我对此进行了更改:
Edited: These code will work on my web: 编辑:这些代码将在我的网站上运行:
$('.a-iframe').attr({
scrolling: "no",
frameborder: "0",
allowtransparency: "true"
});
When use selector, you'll get the array of all that mets the selector, and jquery functions can apply to them all at once. 使用选择器时,您将获得所有符合选择器的数组,而jquery函数可以一次将其全部应用。
$(function() { $('.a-iframe').attr({ scrolling: "no", frameborder: "0", allowtransparency: "true" }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <iframe class="a-iframe" src="https://jsfiddle.net/"></iframe> <iframe data-aa="74836" class="a-iframe" src="https://ad.a-ads.com/74836?size=468x60" style="width:468px;height:60px;border:0px;border-width:0px;border-style:none;border-color:transparent;background-color:#FFFFFF;overflow:hidden !important;padding: 0px !important;margin: 0px !important;"></iframe> <iframe class="a-iframe" src="https://validator.w3.org/check"></iframe>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.