Home  >  Article  >  Web Front-end  >  What to do if the javascript selector fails

What to do if the javascript selector fails

藏色散人
藏色散人Original
2023-02-10 10:15:582331browse

The JavaScript selector fails because the code is not standardized. The solution is: 1. Remove the imported JS code and the ID selector method will be effective; 2. Introduce it before introducing "jquery.js" Just specify the JS code.

What to do if the javascript selector fails

The operating environment of this tutorial: Windows 10 system, javascript version 1.8.5, Dell G3 computer.

What should I do if the javascript selector fails?

jQuery’s ID selector failure problem

jQuery’s ID selector works fine in other files in the same project;

On the current page, other jQuery functions (such as: $(document).ready(function(){ alert("ok"); });) run normally,

but the ID selector method fails, such as alert($("#user")) results in null.

Cause:

The current page has introduced some JS codes, which may be caused by irregularities in these codes.

Solution:

(1) Remove the imported JS code, the ID selector method is valid;

(2) Because these JS codes must be used, so after the introduction These JS codes were introduced before jquery.js, and the ID selector method was tested to be effective.

Related extensions:

#The id selector selects elements with the specified id.

id refers to the id attribute of the HTML element.

Note: The id attribute must be unique within the document.

Note: Do not use id attributes starting with numbers! This may cause problems in some browsers.

Syntax

$("#id")参数:

id Required. Specifies the id of the element to be selected.

Recommended study: "JavaScript Video Tutorial"

The above is the detailed content of What to do if the javascript selector fails. For more information, please follow other related articles on the PHP Chinese website!

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