为什么 jQuery .prop() 不适用于 'allowfullscreen'?

Why is jQuery .prop() not working for 'allowfullscreen'?

根据文档,jQuery .prop() 方法仅适用于 selectedIndextagNamenodeNamenodeTypeownerDocumentdefaultCheckeddefaultSelected.

但据我检查和尝试,它不适用于 iFrame 属性 allowFullscreen

但是,这个属性是布尔值,所以如果我想将它设置为工作或不工作,使用 jQuery .attr() 是不可能的,因为该属性是活动的只要设置了 any 值。因此,唯一的方法是设置属性或(再次)完全删除它,这至少不是最佳方法。


目前我需要设置如下:

allowFullscreen ? $iframe.attr('allowFullscreen', 'true') : $iframe.removeAttr('allowFullscreen');

但它以某种方式无法正常工作normal/easy/logical:

$iframe.prop('allowfullscreen', allowFullscreen);

这是一个错误还是有意为之?

这会将您的属性设置为 true/false,具体取决于您的 allowFullScreen 变量。

$iframe.attr('allowFullScreen', allowFullScreen);

因此您将拥有值为 true 或 false 的属性,您可以像这样检查它们:

var isAll = ($iframe.attr('allowFullScreen') == 'true');

由于历史原因,HTML属性和HTML属性之间的区分比较混乱。 XHTML,是实际的 XML,propertiesdisabled="disabled" 这样它们将解析为有效的 XML。幸运的是,HTML5 把我们从地狱中救了出来。

MDN 文档将 HTML 属性(如 disabled)称为 "Boolean" 属性。

但是,allowfullscreen 被引用为具有可以设置为 true 的值,而不是 "Boolean" 属性。换句话说,allowfullscreen 是一个属性,而不是 属性。

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#Attributes

allowfullscreen
This attribute can be set to true if the frame is allowed to be placed into full screen mode by calling its Element.requestFullscreen() method. If this isn't set, the element can't be placed into full screen mode.

"Boolean"没有提到。 "true" 的价值是另一个赠品...

https://www.w3.org/TR/html5/infrastructure.html#boolean-attribute

Note: The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

如果属性是鸭子,allowfullscreen看起来像鸭子,游泳像鸭子,叫声像鸭子,但不是实际上是一只鸭子。