Home >Web Front-end >JS Tutorial >Issues you need to pay attention to with jquery selectors_jquery

Issues you need to pay attention to with jquery selectors_jquery

WBOY
WBOYOriginal
2016-05-16 16:29:591396browse

Let’s look at a piece of code first, it’s very simple, as follows

Copy code The code is as follows:


111
222
333


Copy code The code is as follows:

$(function() {
$("#button1").click(function() {
​​​​​ $("#div1 span").html("aaa");
});
});

$("#div1 span") gets an array of three objects

1. If you execute $("#div1 span").html("aaa"), all objects in the array will change. As shown below

2. If you execute $("#div1 span").html() and only get the value, only the value of the first object in the array will be taken

So if the selector is an array and you want to operate on each element of the array, it is best to use each().

There are also some things to note

Precautions for special symbols in the selector. The selector contains special characters such as ".", "#", "(" or "]". According to W3C regulations, attribute values ​​cannot contain these special characters. However, in actual projects, we occasionally encounter special characters such as "#" and "." in expressions. If we process them in the normal way, an error will occur

.

The solution to this type of error is to escape using an escape character.

bb

cc

Cannot be written like this:
$('#id#b'); $('#id[1]');
Escape symbols should be used:
$('#id\#b'); //Escape the special character "#"
$('#id\[1\]'); //Escape special characters "[ ]"

Statement:
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