빠른 가이드: jQuery

안녕하세요 프로그래머 여러분!

오늘 우리는 jQuery에 대해 토론하고 탐구할 것입니다! 들어본 적이 있습니까? 예 또는 아니오라고 말하든 jQuery의 기본 사항을 살펴보고 사용할지 여부를 결정할 것이기 때문에 올바른 기사에 도달한 것입니다.

시작하자.


개요



jQuery를 몇 가지 구성 요소로 나눌 것입니다.
  • What is jQuery?
  • Syntax
  • Selectors
  • Attributes
  • Methods
  • Should We Use jQuery?



  • 제이쿼리란?

    jQuery is a fast, small, feature-filled JavaScript library. It makes things like HTML manipulation, CSS manipulation and event handling simple and readable.

    jQuery is used alongside JavaScript and can be downloaded from here 또는 애플리케이션 HTML 파일의 헤드 섹션에 아래 스크립트 태그를 포함할 수 있습니다.
    <script src=”https://code.jquery.com/jquery-3.1.1.js”></script>
    소스 속성에 .js 확장자가 있는 방법에 주목하세요!


    통사론

    Here is the base formula for jQuery syntax:

    $("selector").action()

    • The $ represents access to jQuery.
    • The selector finds a part of HTML either by class name, id or HTML element type.
    • The action() is the action performed on the part of the HTML. Thus, the action() is a jQuery method.

    선택기

    Like I mentioned previously, the selector finds a part of the HTML based on either class name, id or HTML element type.

    Let's take a look at some examples:

    $("div") // => selects all div elements
    $(".menu") // => selects element with class name of "menu"
    $("#header") // => selects element with id of "header"
    $("form.search") // => selects <form> element with class name of "search"
    $("p:first") // => selects the first <p> element
    $(“div p”) => selects all <p> elements that are children of a <div> element

    The main purpose of the selector is to retrieve an HTML element from the HTML file to be referenced or manipulated in the JavaScript file.

    If you are familiar with JavaScript, then you may realize this is not too different than using document.querySelector() or document.getElementById() . If you realized this, good job! You are making connections and that is amazing.


    속성

    Since jQuery helps to manipulate both HTML and CSS, jQuery uses various methods to access attributes of an HTML element to make these changes.

    The most common attributes we may see include href, src, class, styles, id etc. And using jQuery, we can add, remove or edit these attributes.

    This is how we would go about adding a href attribute to an 'a' tag:

    $("a").attr("href", "www.google.com")
    
  • a represents the 'a' tag; the selector.

  • .attr() is the method to set an attribute; it takes in two (2) parameters:

    1. the type of attribute
    2. the value of the attribute
  • "href" is the type of attribute and " www.google.com "는 값입니다.

  • 이제 ''' 태그를 검사하면 다음과 같이 표시됩니다.

             `<a href="www.google.com"></a>`
    

    속성을 제거하려면 속성 유형을 매개변수로 받는 removeAttr()을 사용할 수 있습니다.

    지금까지 배운 내용을 모아서 시도해 봅시다.

    let x = `
      <input id="input"
      placeholder="search your saved items">
    `
    // add a class attribute
    $("input").attr("class", "search")
    
    // remove the id attribute
    $("input").removeAttr("id")
    
    


    콘솔에서 이것을 시도하십시오! 당신은 무엇을 얻을? 이 같은?
    <input class="search" placeholder="search your saved items">
    시원한! 이제 jQuery의 큰 부분은 JavaScript 파일에서 HTML을 조작할 수 있도록 주어진 HTML 요소의 속성에 액세스하는 기능이라는 것을 알고 있습니다.


    행동 양식

    So far, we have seen some methods regarding the attributes of HTML elements. However, there are so many more to discover. Today let's go over a few:

    • The .html() method is used to retrieve OR change the content of the selected element, including the HTML markup.

    • The .text() method is used to retrieve OR change the text content of the selected element.

    • The .css() method can be used to get and set CSS properties.

      • To set multiple CSS properties, the .css() method uses JSON syntax.
      • Example: $(“p”).css({“color”:”red”, “font-size”:”16px”})
    • The .val() method allows us to get AND set the values of form fields, such as textboxes, dropdowns and inputs.

    • The .append() method inserts content at the end of the selected element(s).

    • The .prepend() method inserts content at the beginning of the selected element(s).

    • The .after() method inserts content with HTML markup after the selected element(s).

    • The .before() method inserts content before with HTML markup the selected element(s).

    • The .addClass() method adds a class to the element called on.

      • When specifying multiple class names, separate them using spaces.
    • The .removeClass() method removes the class of the element called on.

    • The .toggleClass() method toggles between adding and removing classes from selected elements. If the specified class exists already, it is then removed. If the specified class does not exist, it is added.

    • The .width() and .height() methods can be used to get and set the width and height of HTML elements.

    ** I recommend you try a few of these on your own. Then, I recommend finding other methods used with jQuery and trying those as well. **


    jQuery를 사용해야 합니까?

    Now, this may be a VERY simple and understated guide to jQuery, but it is important! We need to understand the base fundamentals of certain languages, libraries, packages... to best understand how to use them, why we use them AND if we should use them.

    jQuery is a great library to use if you want to DRY your code and work on readability. It is also a great tool that shows the bridge between multiple files (.html, .css, .js), especially in beginner projects and applications.

    However, with the introduction to and the heavy use of JavaScript frameworks such as ReactJS, Vue.js, and AngularJS, jQuery is not totally necessary anymore. Its syntax does not belong within the inner workings of these frameworks.

    But let's beg the question: "Should we still use it?"

    Yes, it is worth learning jQuery even in 2022: jQuery is used by many existing and efficiently-operating websites! While new web applications created this year may not utilize jQuery, jQuery was once one of the most widely used JavaScript libraries before the introduction to frameworks such as React, Angular and Vue.



    Let's continue to learn, grow and teach as we continue on our personal journeys. 🤍🤍

    Please feel free to leave comments, questions and suggestions below. And follow me for more content on JavaScript, ReactJS, HTML and CSS. 🤍🤍

    좋은 웹페이지 즐겨찾기