Home Web Front-end JS Tutorial Using DOM operations to replace regular expressions in jQuery_jquery

Using DOM operations to replace regular expressions in jQuery_jquery

May 16, 2016 pm 04:23 PM
DOM operation jquery regular expression

In today's world where B/S structure clients are becoming more and more "fat", as a full-end programmer, you are likely to operate html strings on the front end. Note that you are operating html strings, not the current page. html.

For example, Ueditor, an online HTML rich text editor launched by Baidu, can create rich text documents online, and its functions are comparable to a streamlined version of Microsoft Word. Although Ueditor has the aura of Baidu, the actual effect is not very satisfactory. We need to process the html string it generates twice, such as setting the width of all images to 90%.

Through a certain method, we can get the html string in the text editor. Assume that the string is as follows:

Copy code The code is as follows:

Flower-like Sao Nian


Sao Nian Selfie

Mysterious Pyramid


Chinese Pyramid

A dream-like life


All kinds of life

But what to do next? The elegant processing of strings makes it easy for us to think of regular expressions. Can we use regular expressions here?

The answer is yes, try the regular effect first. Set the width of all images to 90%. The simplest way is to add the style attribute to the img tag and then specify the width in style.

Using regular rules, the first step is to match all img tags. Since the img tag does not necessarily have a style attribute, you must first determine whether there is a style attribute, and then directly add width: 90%; to the style attribute? No, this may overwrite other original attributes, so just add them directly. The addition will not overwrite them! Still not working, what if there is width originally. . .

I haven’t started writing regular expressions yet. Thinking about the process first, it is already very tedious. In fact, the implementation is even more complicated.

Fortunately, we can change our thinking and solve this problem with the help of jQuery.

The power of jQuery is that it can directly package an HTML string into a dom element. This dom element does not exist in the current page, it is placed in memory.

Through jQuery, you only need this piece of code:

Copy code The code is as follows:

//Define the container to facilitate obtaining the modified html string
//Use jQuery directly to wrap "
". At this time, there will be a div element in the memory
var $container = $("
");
//Assume this is the html string that needs to be modified
var html = "";
//Directly insert the html string to be modified into the container
//jQuery is powerful enough to automatically wrap html strings into dom elements and then insert them into the div container in memory
$container.append(html);
//Search for all img tags in the container and traverse
$container.find("img").each(function(i,n){
//For each img element, directly modify the width attribute in its style attribute
//If the style attribute does not exist, add it automatically; if there is already a width attribute, modify it directly; no need to worry too much
$(n).css({
       width: "90%"
});
});
//Get the modified html string, which is the html content of the container
alert($container.html());

The comments in the code are very detailed, so I won’t explain too much. We need to understand that jQuery can not only operate the html in the real page, but also the virtual html in the memory.

By comparing the two, I believe readers can immediately understand which method is better.

As Xiao Cai often says: If you think a problem can be solved, but it still hasn’t been solved after a long time, it’s probably that your thinking is wrong. Think about it from another angle, and the problem will be solved!

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 admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1249
24
PHP regular expression validation: number format detection PHP regular expression validation: number format detection Mar 21, 2024 am 09:45 AM

PHP regular expression verification: Number format detection When writing PHP programs, it is often necessary to verify the data entered by the user. One of the common verifications is to check whether the data conforms to the specified number format. In PHP, you can use regular expressions to achieve this kind of validation. This article will introduce how to use PHP regular expressions to verify number formats and provide specific code examples. First, let’s look at common number format validation requirements: Integers: only contain numbers 0-9, can start with a plus or minus sign, and do not contain decimal points. floating point

How to match timestamps using regular expressions in Go? How to match timestamps using regular expressions in Go? Jun 02, 2024 am 09:00 AM

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

How to validate email address in Golang using regular expression? How to validate email address in Golang using regular expression? May 31, 2024 pm 01:04 PM

To validate email addresses in Golang using regular expressions, follow these steps: Use regexp.MustCompile to create a regular expression pattern that matches valid email address formats. Use the MatchString function to check whether a string matches a pattern. This pattern covers most valid email address formats, including: Local usernames can contain letters, numbers, and special characters: !.#$%&'*+/=?^_{|}~-`Domain names must contain at least One letter, followed by letters, numbers, or hyphens. The top-level domain (TLD) cannot be longer than 63 characters.

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to verify password using regular expression in Go? How to verify password using regular expression in Go? Jun 02, 2024 pm 07:31 PM

The method of using regular expressions to verify passwords in Go is as follows: Define a regular expression pattern that meets the minimum password requirements: at least 8 characters, including lowercase letters, uppercase letters, numbers, and special characters. Compile regular expression patterns using the MustCompile function from the regexp package. Use the MatchString method to test whether the input string matches a regular expression pattern.

Chinese character filtering: PHP regular expression practice Chinese character filtering: PHP regular expression practice Mar 24, 2024 pm 04:48 PM

PHP is a widely used programming language, especially popular in the field of web development. In the process of web development, we often encounter the need to filter and verify text input by users, among which character filtering is a very important operation. This article will introduce how to use regular expressions in PHP to implement Chinese character filtering, and give specific code examples. First of all, we need to clarify that the Unicode range of Chinese characters is from u4e00 to u9fa5, that is, all Chinese characters are in this range.

Introduction to how to add new rows to a table using jQuery Introduction to how to add new rows to a table using jQuery Feb 29, 2024 am 08:12 AM

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:

See all articles