Home > Web Front-end > JS Tutorial > body text

Let's talk about JavaScript event delegation, judging whether the URL is legal and fully arranged.

WBOY
Release: 2022-11-16 20:50:35
forward
1466 people have browsed it

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.

Let's talk about JavaScript event delegation, judging whether the URL is legal and fully arranged.

【Related recommendations: JavaScript video tutorial, web front-end

1. Event delegation

Effect demonstration

Lets talk about JavaScript event delegation, judging whether the URL is legal and fully arranged.

##Requirements

Complete

JavaScript 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 code
nbsp;html>
    
    
    
    事件委托
    
Copy after login
            
  • .
  •         
  • .
  •         
  • .
  •     
    
This question is quite simple. The principle of event delegation actually uses the

event bubbling

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

HTTP(S)

Hand-shredded code
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)}
Copy after login
This question examines the regularity Application, using a lot of regular knowledge, including:

  • ^

    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
format is as follows:

Protocol part
    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, etc., are 2-6 digits: expressed as ([a-zA-Z]{2,6})

    Port part: expressed as
  • (:\d)?
  • ##The request path is such as

    /login
  • : Represented as
  • (\/.*)?

    Question mark and hash value such as

    ?age=1
  • ,
  • #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:

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!