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

The difference and usage between eq and get in jquery_jquery

WBOY
Release: 2016-05-16 18:07:48
Original
1334 people have browsed it

For example:

Feiyu

Use eq to get the color value of the first p tag:
$("p").eq (0).css("color") //Because eq(num) returns a jq object, you can use the jq method css to use get to get the color value of the first p tag:
$("p ").get(0).style.color //Because get(num) returns an html object, the traditional HTML object method must be used, and the jq object is useless at this time. Of course, you can also get(num) and convert the object to a jq object before performing the operation:
$($("p").get(0)).css("color")----- -------------------------------------------------- ------------------
more eq
see:
http://api.jquery.com/eq/
--- -------------------------------------------------- ---------------------
more get:
see:
http://api.jquery.com/get/
eq: The return is a jquery object whose function is to reduce the set of matched elements to one element. The position of this element in the set of matching elements becomes 0, and the length of the set becomes 1
get: is an array of html objects that is used to obtain one of the matching elements. num indicates which matching element is obtained.

For example: html code

Copy code The code is as follows:

< ul>
  • li-1

  • li-2



  • For example, we pass jquery selector $("li") Then we will have two li elements. How can I only select one of them?

    $("li:eq(0)").html() or $ ("li").eq(0).html() is the first li. Here we will get li-1
    $("li:eq(1)").html() or $("li" ).eq(1).html() is the second li. Here we will get li-2

    Let’s look at get. Because get returns an html object, here we are
    $("li" ).get(0).style.color='red'
    Only used in this way or convert the object returned by get into a jquery object before operating
    $($("li").get(0)).css ("color",'red') can

    Complete code
    Copy code The code is as follows:












    • li-2




    Related labels:
    source:php.cn
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template