


Let's talk about JavaScript event delegation, judging whether the URL is legal and fully arranged.
This article brings you relevant knowledge about JavaScript, which mainly introduces the related content of event delegation, judging whether the URL is legal and full arrangement. Let’s take a look at it together. I hope it will be helpful to everyone. helpful.
【Related recommendations: JavaScript video tutorial, web front-end】
1. Event delegation
Effect demonstration
##Requirements
CompleteJavaScript Code, the requirements are as follows:
Add a click event to the "ul
" tag
- When a "
li
" label is clicked, the content of the label is spliced with "
." symbols. For example: when a "
li" tag is clicked, the content of the tag is "
.."
## Tear the codenbsp;html>
<meta>
<meta>
<meta>
<title>事件委托</title>
Copy after login
- .
- .
- .
<script>
// 补全代码
const ul = document.getElementsByTagName('ul')[0]
ul.onclick = function (e) {
const tar = (e || window.event).target // 如果点击的对象的名称是li
// localName 获取标签名
if (tar.localName === 'li') {
tar.innerHTML = tar.innerHTML + '.'
}
}
</script>This question is quite simple. The principle of event delegation actually uses the
event bubblingnbsp;html> <meta> <meta> <meta> <title>事件委托</title>
mechanism.
Event bubbling: triggering a certain type of event (such as a click event) on an object. If the object defines a handler for this event, then the event will call this handler. If this event handler is not defined or the event returns true, then this event will be propagated to the parent object of this object, from inside to outside, until it is handled (all similar events of the parent object will be activated), or it Reached the top level of the object hierarchy, that is, the document object (some browsers)In actual development, event delegation is used to uniformly capture and process events by the parent class, which can reduce the duplication of subclass events definition.
2. Determine whether the URL is legal.
Requires to complete the
JavaScript code and requires The format of Boolean
returns whether the string parameter is in the legal URL
format. Note: The protocol is only
Hand-shredded codeconst _isUrl = url => {
// 补全代码
let reg = /^((https|http):\/\/)?(([A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\.)+([A-Za-z]{2,6})(:\d+)?(\/.*)?(\?.*)?(#.*)?$/g;
return reg.test(url)}
Copy after loginThis question examines the regularity Application, using a lot of regular knowledge, including:
const _isUrl = url => { // 补全代码 let reg = /^((https|http):\/\/)?(([A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\.)+([A-Za-z]{2,6})(:\d+)?(\/.*)?(\?.*)?(#.*)?$/g; return reg.test(url)}
- ^
represents the beginning
- [A- Za-z0-9]
means matching uppercase and lowercase letters and numbers
- \/
means matching
/
, because in the regular expression/
has other meanings, so to match/
you need to escape it with\
- ?
is equivalent to
{0,1}
, which means it appears once or not. -
means it appears at least once It is 1
- |
(pipe character), which means
or
, indicating that it matches the content on both sides of | Any one of - \.
means matching
.
, just like/
, it must match.
Needs to be escaped with\
in front - {n,m}
means n-m times
- \d
Matching numbers
- .
is
[^\n\r\u2028\u2029]
is a wildcard character that represents almost any character - *
represents 0 or more occurrences
- .*
is to match any multiple arbitrary characters
- $
represents the end
- g
represents global matching
##The legal URL
- http (s)
- Optional: expressed as
((https|http):\/\/)?
Domain name part: expressed as (([A-Za-z0-9] -[A-Za-z0-9] |[A-Za-z0-9] )\.) -
Top-level domain names, such as com - ,
cn
Port part: expressed as, etc., are 2-6 digits: expressed as
([a-zA-Z]{2,6}) (:\d)? -
/login##The request path is such as
: Represented as - (\/.*)?
?age=1Question mark and hash value such as
, - #dom
: expressed as
(\?.*)?
and(#.*)?
3. Full arrangement
Requires
to complete the JavaScript code and requires all permutations and combinations of string parameters to be returned in the form of an array.
Note:
The characters in the string parameter are not repeated and only contain lowercase letters
The returned permutation and combination array does not distinguish the order
-
Example:
输入:_permute('abc') 输出:['abc','acb','bac','bca','cab','cba']
Copy after login
Hand-shredded code
Full arrangement is one of the more common algorithms, and there are many solutions. Here is a very clever solution for you: const _permute = string => {
// 补全代
const res = []; // 结果数组
function search(str) {
console.log('1', 'str=' + str);
// 如果长度相等了就存放到结果数组中
if (str.length === string.length) {
res.push(str)
}
// 遍历string
for (let char of string) {
console.log('2', 'str=' + str, 'char=' + char);
// 如果str内不含char,就使用str+char开始递归
if (str.indexOf(char) <p>整体思路就是运用循环加递归,但这个过程中涉及到了<code>JavaScript</code>中循环的执行机制,我们以执行<code>console.log(_permute('ab'));</code>为例查看控制台打印结果:</p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/067/66e36e8cc39d1c0f975c61596092ab88-3.png" class="lazy" alt="Lets talk about JavaScript event delegation, judging whether the URL is legal and fully arranged."></p><p><code>search</code>函数中的<code>for</code>循环执行次数与<code>string</code>参数的长度相等,此时传递的<code>string</code>参数为<code>ab</code>,长度为2,即<code>search</code>函数中的<code>for</code>循环会执行两次。</p><p><strong>这里需要注意的就是:<code>for</code>循环中执行的递归(再次调用<code>search</code>函数)并不会中断当前的<code>for</code>循环,只会将还未执行的循环暂且搁置,等到系统空闲时这些搁置的循环才会开始执行</strong>( <code>javascript</code> 引擎在同一时刻只能处理一个任务,即<strong>单线程</strong>),具体过程见下方图解:</p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/067/6cdcbaa13171acb1aed4f70f827454d2-4.png" class="lazy" alt="Lets talk about JavaScript event delegation, judging whether the URL is legal and fully arranged."></p><p>【相关推荐:<a href="https://www.php.cn/course/list/17.html" target="_blank" textvalue="javascript视频教程">JavaScript视频教程</a>、<a href="https://www.php.cn/course/list/1.html" target="_blank">web前端</a>】</p>
Copy after login
const _permute = string => { // 补全代 const res = []; // 结果数组 function search(str) { console.log('1', 'str=' + str); // 如果长度相等了就存放到结果数组中 if (str.length === string.length) { res.push(str) } // 遍历string for (let char of string) { console.log('2', 'str=' + str, 'char=' + char); // 如果str内不含char,就使用str+char开始递归 if (str.indexOf(char) <p>整体思路就是运用循环加递归,但这个过程中涉及到了<code>JavaScript</code>中循环的执行机制,我们以执行<code>console.log(_permute('ab'));</code>为例查看控制台打印结果:</p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/067/66e36e8cc39d1c0f975c61596092ab88-3.png" class="lazy" alt="Lets talk about JavaScript event delegation, judging whether the URL is legal and fully arranged."></p><p><code>search</code>函数中的<code>for</code>循环执行次数与<code>string</code>参数的长度相等,此时传递的<code>string</code>参数为<code>ab</code>,长度为2,即<code>search</code>函数中的<code>for</code>循环会执行两次。</p><p><strong>这里需要注意的就是:<code>for</code>循环中执行的递归(再次调用<code>search</code>函数)并不会中断当前的<code>for</code>循环,只会将还未执行的循环暂且搁置,等到系统空闲时这些搁置的循环才会开始执行</strong>( <code>javascript</code> 引擎在同一时刻只能处理一个任务,即<strong>单线程</strong>),具体过程见下方图解:</p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/067/6cdcbaa13171acb1aed4f70f827454d2-4.png" class="lazy" alt="Lets talk about JavaScript event delegation, judging whether the URL is legal and fully arranged."></p><p>【相关推荐:<a href="https://www.php.cn/course/list/17.html" target="_blank" textvalue="javascript视频教程">JavaScript视频教程</a>、<a href="https://www.php.cn/course/list/1.html" target="_blank">web前端</a>】</p>
The above is the detailed content of Let's talk about JavaScript event delegation, judging whether the URL is legal and fully arranged.. 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

AI Hentai Generator
Generate AI Hentai for free.

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 use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
