请注意,.attributes只获取当前属性,这不是这个问题的内容。
我想要一种获取DOM 元素所有属性的方法。不仅是现在的那些,还有未来可能的那些。
特定的用例是在 anSVGElement中找到不在an中的潜在属性HTMLElement- MDN 上有一个列表(SVG 属性参考),但是,出于显而易见的原因,硬编码不是一个好主意。
我最初的想法是遍历每个实例的原型链并比较两个列表(对事件处理程序进行基本过滤),但这实际上并没有给出潜在的 svg 属性。
编辑
document.body作为目标应显示超过50属性的列表,包括喜欢的东西contentEditable,contextMenu,dir,style,等。在css中进行缩放代码(因为它不依赖于javascript),所以宽度/高度比在所有大小都保持不变.对于所有当前支持的浏览器,它可以工作,但IE11似乎完全忽略了媒体查询 - 有没有人遇到过让它工作的方法?
使用scss轻松编码 -
$width: 512;
$height: 350;
@mixin aspect-ratio() {
$widthRatio: $width / $height;
$heightRatio: $height / $width;
$widthHeight: #{$widthRatio * 100}vh;
$heightWidth: #{$heightRatio * 100}vw;
/* Fix the width relative to the height */
@media all and (min-width: $widthHeight) {
max-width: $widthHeight;
}
/* Fix the height relative to the width */
@media all and (min-height: $heightWidth) {
max-height: $heightWidth;
}
}
body {
@include aspect-ratio();
}
Run Code Online (Sandbox Code Playgroud)
哪个输出 -
@media all and (min-width: 146.28571vh) {
body { …Run Code Online (Sandbox Code Playgroud)