jQuery 속성 추가

Sheeraz Gul 2024년2월15일
jQuery 속성 추가

jQuery attr() 메소드는 HTML 요소에 속성을 추가하는 데 사용됩니다. 이 튜토리얼은 jQuery에서 attr() 메소드를 사용하는 방법을 보여줍니다.

jQuery 속성 추가

jQuery의 attr() 메소드를 사용하여 HTML 요소에 속성을 추가할 수 있습니다. 이 방법의 구문은 다음과 같습니다.

$(selector).attr("property", "Value")

어디에:

  1. selector는 ID, 클래스 또는 요소 이름이 될 수 있습니다.
  2. property는 속성의 이름입니다.
  3. Value은 속성의 값입니다.

예를 들어 보겠습니다.

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <title>jQuery Add Attribute</title>
      <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
      <script>
         $(document).ready(function(){
             $("#attr").click(function(){
                 $('input[type="checkbox"]').attr("checked", "checked");
             });
         });
      </script>
   </head>
   <body>
      <button type="button" id="attr">Click to Select the Checkbox</button>
      <input type="checkbox">
   </body>
</html>

위의 코드는 체크되지 않은 체크박스를 체크할 것입니다. 코드는 속성이 확인되고 값도 확인되는 attr() 메서드와 함께 요소 이름 선택기를 사용합니다.

출력 보기:

속성 추가

작가: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

관련 문장 - jQuery Attribute