728x90

.scrollTop()

.scrollTop()은 선택한 요소의 스크롤바 수직 위치를 반환하거나 스크롤바 수직 위치를 정한다.

 

예제

$( 'div' ).scrollTop();

맨위로 이동 시킬 경우 .scrollTop(0);

요소는 상위 요소로 잡아야한다

 

자바스크립트는 Window.scrollTo() 사용

https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo

 

아래와 같이 옵션 줄 수 있음

window.scrollTo({
  top: 100,
  left: 100,
  behavior: 'smooth'
});
728x90
728x90

[정의]

.show(). / .hide() 

.show()는 요소의 css가 display:none; 일 때 요소를 display:block;으로 바꿔주는 함수
.hide()는 display:block;->display:none; 

 

[예제]

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#hide").click(function(){
    $("p").hide();
  });
  $("#show").click(function(){
    $("p").show();
  });
});
</script>
</head>
<body>

<p>If you click on the "Hide" button, I will disappear.</p>

<button id="hide">Hide</button>
<button id="show">Show</button>

</body>
</html>

 

[구현]

 

 

출처 : https://www.w3schools.com/jquery/jquery_hide_show.asp

 

jQuery Effects - Hide and Show

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

728x90
728x90

prop()

[정의]

.prop()는 지정한 선택자를 가진 첫번째 요소의 속성값을 가져오거나 속성값을 추가함

js attr은 html요소를 가져오므로 다름

 

[문법]

속성값 가져오기

.prop( propertyName )

 

속성값 추가하기

.prop( propertyName, value )

 

[예제]

a 요소의 href 속성값을 .attr()과 .prop()로 가져와서 출력합니다.

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
  </head>
  <body>
    <p><a href="#">Link</a></p>
    <p>
      <script>
        document.write( $( 'a' ).attr( 'href' ) );
        document.write( '<br>' );
        document.write( $( 'a' ).prop( 'href' ) );
      </script>
    </p>
  </body>
</html>

 

예제 2

제일 위의 체크박스를 체크하거나 체크 해제를 하면 아래의 모든 체크박스가 체크되거나 체크 해제됩니다.

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
      $( document ).ready( function() {
        $( '.check-all' ).click( function() {
          $( '.ab' ).prop( 'checked', this.checked );
        } );
      } );
    </script>
  </head>
  <body>
    <form>
      <p><input type="checkbox" name="all" class="check-all"> <label>Check ALL</label></p>
      <hr>
      <p><input type="checkbox" name="cb1" class="ab"> <label>Checkbox 1</label></p>
      <p><input type="checkbox" name="cb2" class="ab"> <label>Checkbox 2</label></p>
    </form>
  </body>
</html>

참고 : https://www.codingfactory.net/10341

728x90

+ Recent posts