不应以牺牲质量赢取覆盖率
<form id="form" action="/conceal">
<input type="text" id="action">
<input type="submit" id="submit">
</form>
<script>
// 预期返回form标签的action属性,返回的却是input标签对象
console.log(document.getElementById("form").action);
// <input type="text" id="action">
document.getElementById("form").submit();
// Uncaught TypeError: document.getElementById(...).submit is not a function
// 原因:浏览器将<form>元素内所有input标签都作为表单form的属性
// 添加到form属性的名称是input元素的id或name属性
// 当form本身的属性与input元素的id或name属性冲突时
// form本身的属性就被input元素的id或name属性替换了
// from.submit属性也发生了同样的情况
</script>
// ignore negative width and height values(来之jQuery)
if ((key == "width" || key == "height") && parseFloat(value) < 0) value = undefined;
// 将width或height值设置为负数时,在IE某些版本会报错,在其他版本或其他浏览器会被忽略
// 这行代码会被大多数浏览器无任何影响,只对抛出错误的IE浏览器产生作用
// 代码来自jQuery
// input元素的type属性本身作为DOM的一部分,在IE浏览器内部是不允许修改的,修改会抛出异常
// jQuery给出中间方案,所有浏览器上都不允许修改input的type属性,并抛出统一的异常信息
if (name == "type" && elem.nodeName.toLowCase() == "input" && elem.parentNode) throw "type attribute can‘t be changed";
// jQuery团队认为:一致的API、一致的行为,比开发跨浏览器代码更重要
// 如果浏览器不支持find方法(ES6新增)
// 以下是MDN提供的垫片
if (!Array.prototype.find) { // 特征检测
// 定义find的实现
Array.prototype.find = function(predicate) {
if (this == null) {
throw new TypeError("find called on null or undefined");
}
if (typeof predicate !== "function") {
throw new TypeError("predicate must be a function");
}
var list = Object(this);
// 确保length是非负整数
// >>> 是补零右移运算符,将第一个操作数向右移动指定的位数,丢弃多余的部分
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
// 查找数组中满足指定条件的第一个元素
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
return undefined;
}
}
原文:https://www.cnblogs.com/hycstar/p/14059024.html