Home >Web Front-end >JS Tutorial >Knowledge sharing on web front-end
For the expression in the brackets of the if statement, ECMAScript will automatically call Boolean() transformation The function converts the result of this expression into a Boolean value. If the value is true, the following statement is executed, otherwise it is not executed.
Use the length attribute of the arguments object to intelligently determine how many parameters there are, and then apply the parameters reasonably.
For example, to implement an addition operation, accumulate all the numbers passed in, but the number of numbers is uncertain.
function box() {var sum = 0;if (arguments.length == 0) return sum; //如果没有参数,退出for(var i = 0;i < arguments.length; i++) { //如果有,就累加sum = sum + arguments[i]; }return sum; //返回累加结果 } alert(box(5,9,12));
function box(num) {if (num <= 1) {return 1; } else {return num * arguments.callee(num-1);//使用 callee 来执行自身 } }
##It is W3C’s practice to directly receive the event object , IE does not support it. IE defines an event object by itself, which can be obtained directly from window.event.
input.onclick = function (evt) {var e = evt || window.event; //实现跨浏览器兼容获取 event 对象 alert(e); };
target in W3C and srcElement in IE both represent the event's target Target.
function getTarget(evt) {var e = evt || window.event;return e.target || e.srcElement; //兼容得到事件目标 DOM 对象} document.onclick = function (evt) {var target = getTarget(evt); alert(target); };
In the process of preventing event bubbling , W3C and IE adopt different methods, then we must make compatibility.
function stopPro(evt) {var e = evt || window.event; window.event ? e.cancelBubble = true : e.stopPropagation(); }
function preDef(evt) {var e = evt || window.event;if (e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; } }
When we right-click on the web page, the menu that comes with windows will automatically appear. Then we can use the contextmenu event to modify the menu we specify, but only if the default behavior of right-click is cancelled.
function addEvent(obj, type, fn) { //添加事件兼容if (obj.addEventListener) { obj.addEventListener(type, fn); } else if (obj.attachEvent) { obj.attachEvent('on' + type, fn); } }function removeEvent(obj, type, fn) { //移除事件兼容if (obj.removeEventListener) { ob j.removeEventListener(type, fn); } else if (obj.detachEvent) { obj.detachEvent('on' + type, fn); } } addEvent(window, 'load', function () {var text = document.getElementById('text'); addEvent(text, 'contextmenu', function (evt) {var e = evt || window.event; preDef(e);var menu = document.getElementById('menu'); menu.style.left = e.clientX + 'px'; menu.style.top = e.clientY + 'px'; menu.style.visibility = 'visible'; addEvent(document, 'click', function () { document.getElementById('myMenu').style.visibility = 'hidden'; }); }); });
BackCompat and CSS1Compat.
BackCompat: Standard compatibility mode is turned off. The width of the browser client area is document.body.clientWidth;CSS1Compat: Standards compatibility mode is turned on. The browser client area width is document.documentElement.clientWidth. For example:function getViewport(){ if (document.compatMode == "BackCompat"){ return { width: document.body.clientWidth, height: document.body.clientHeight } } else { return { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight } } }
function getStyle(element, attr) {if (typeof window.getComputedStyle != 'undefined') {//W3Creturn window.getComputedStyle(element, null)[attr]; } else if (typeof element.currentStyle != 'undeinfed') {//IEreturn element.currentStyle[attr]; } }11.
12.
//跨浏览器获取视口大小function getInner() {if (typeof window.innerWidth != 'undefined') { //IE8及以下undefinedreturn { width : window.innerWidth, height : window.innerHeight } } else {return { width : document.documentElement.clientWidth, height : document.documentElement.clientHeight } } }
The above is the detailed content of Knowledge sharing on web front-end. For more information, please follow other related articles on the PHP Chinese website!