Improved jquery processing mechanism and improved jquery Mechanism

Source: Internet
Author: User

Improved jquery processing mechanism and improved jquery Mechanism

Using the jQuery selector is much simpler than using the traditional getElementById () and getElementsByTagName () functions, and can avoid certain errors. See the following example:

 <script>  document.getElementById("div").style.color ="red"; </script>

After running the code above, the browser reports an error because the webpage does not have an element with the ID as div.

The improved code is as follows:

<Script> if (document. getElementById ("div") {// use the IF statement to determine whether there is an element whose ID is div. IF yes, run the following code document. getElementById ("div "). style. color = "red"} </script>

In this way, you can avoid errors reported by the browser. However, if there are many elements to be operated on, you may have to make a judgment on each element. The jquery processing is very good, that is, using JQUERY to retrieve elements that do not exist in a webpage does not return an error.

The Code is as follows:

 <script>  $("#div").css("color","red"); </script>

With this precaution, even if you delete a previously used element on a webpage for some reason in the future, you don't have to worry about the JavaScript error of this webpage.

Note:

$ ("Div") is always a jquery object, even if this element is not found on the webpage. Therefore, when jquery is used to check whether an element exists on a webpage.

You cannot use the following code:

<Script> if ($ ("# div") {$ ("# div" ).css ("color", red) // This browser reports an error} </script>

It should be determined by obtaining the length.

The Code is as follows:

<script> if($("#div").length >0){   $("#div").css("color",red) }</script>

At this time, it can also be converted to a DOM object for judgment.

The Code is as follows:

<Body> <div id = "div"> ccccccc </div> <script src = "jquery-2.1.4.min.js"> </script> <script> var $ div = $ ("# div "); var div = $ div [0]; if (div) {div div.css ("color", "red ") // The Color of the DIV changes to red. </script> </body>

This is the perfect processing mechanism of jquery. I hope it will be helpful for you to learn about jquery programming.

Articles you may be interested in:
  • Code for binding event processing functions to elements dynamically added by jQuery
  • Details about asynchronous processing of Json data using Jquery Ajax.
  • Details about asynchronous processing of Json data using jQuery Ajax
  • Jquery serialized form json data returned after ajax submission
  • Example of how jQuery mouse goes through (hover) event delay processing
  • Causes and solutions for not entering callback after Jquery Post Processing
  • Jquery ajax Request Method and prompt that the user is processing. Please wait.
  • If jquery + ajax verification fails, the system submits the form for troubleshooting.
  • How to handle the Uncaught TypeError: Illegal invocation error reported by JQuery
  • Solutions for coexistence of multiple jQuery versions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.