Knowledge sharing on web front-end
1. If conditional statement related
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.
2. Arguments related
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));
3. Internal properties of function
function box(num) {if (num <= 1) {return 1; } else {return num * arguments.callee(num-1);//使用 callee 来执行自身 } }
4. event object
##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); };
5. The target of the event
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); };
6.Prevent event bubbling
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(); }
7. Block event default behavior
function preDef(evt) {var e = evt || window.event;if (e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; } }
8. Context menu event: contextmenu
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'; }); }); });
Document mode in 9.js-document.compatMode
Document mode seems to be rarely used in development. The most common one is when getting the page width and height, such as document width and height, visible area width and height, etc.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 } } }
10. Get Style across browsers
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]; } }
js dynamically inserts css-related styleSheets, insertRule, addRule, and delete styles: deleteRule, removeRule
## Standard browsers support insertRule, and lower versions of IE support addRule.12.
Get the width and height of the page##window.innerWidth, window.innerHeight and document.documentElement.clientWidth , document.documentElement.clientHeight
//跨浏览器获取视口大小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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

Notes deleted from Xiaohongshu cannot be recovered. As a knowledge sharing and shopping platform, Xiaohongshu provides users with the function of recording notes and collecting useful information. According to Xiaohongshu’s official statement, deleted notes cannot be recovered. The Xiaohongshu platform does not provide a dedicated note recovery function. This means that once a note is deleted in Xiaohongshu, whether it is accidentally deleted or for other reasons, it is generally impossible to retrieve the deleted content from the platform. If you encounter special circumstances, you can try to contact Xiaohongshu’s customer service team to see if they can help solve the problem.

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

Link AppleNotes on iPhone using the Add Link feature. Notes: You can only create links between Apple Notes on iPhone if you have iOS17 installed. Open the Notes app on your iPhone. Now, open the note where you want to add the link. You can also choose to create a new note. Click anywhere on the screen. This will show you a menu. Click the arrow on the right to see the "Add link" option. click it. Now you can type the name of the note or the web page URL. Then, click Done in the upper right corner and the added link will appear in the note. If you want to add a link to a word, just double-click the word to select it, select "Add Link" and press

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

Introduction to Caddy Caddy is a powerful and highly scalable web server that currently has 38K+ stars on Github. Caddy is written in Go language and can be used for static resource hosting and reverse proxy. Caddy has the following main features: Compared with the complex configuration of Nginx, its original Caddyfile configuration is very simple; it can dynamically modify the configuration through the AdminAPI it provides; it supports automated HTTPS configuration by default, and can automatically apply for HTTPS certificates and configure it; it can be expanded to data Tens of thousands of sites; can be executed anywhere with no additional dependencies; written in Go language, memory safety is more guaranteed. First of all, we install it directly in CentO

Face-blocking barrage means that a large number of barrages float by without blocking the person in the video, making it look like they are floating from behind the person. Machine learning has been popular for several years, but many people don’t know that these capabilities can also be run in browsers. This article introduces the practical optimization process in video barrages. At the end of the article, it lists some applicable scenarios for this solution, hoping to open it up. Some ideas. mediapipeDemo (https://google.github.io/mediapipe/) demonstrates the mainstream implementation principle of face-blocking barrage on-demand up upload. The server background calculation extracts the portrait area in the video screen, and converts it into svg storage while the client plays the video. Download svg from the server and combine it with barrage, portrait

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are
