본문 바로가기
728x90
300x250
SMALL

javaScript545

지역변수 전역변수 차이 지역변수(Local Variable)와 전역변수(Global Variable)의 차이는 만들어지는 위치이다. 만드는 방법은 동일하지만 어느 위치에 만들어지냐에 따라서 전역변수와 지역변수로 구분되게 된다. 아래 예시 코드는 1번부터 4번까지 모두 같은 이름의 fruit 변수를 출력한다. let fruit = "사과"; function fruitFunction1() { let fruit = "바나나"; document.write(`2번 : ${fruit} `); } function fruitFunction2() { fruit = "딸기"; document.write(`3번 : ${fruit} `); } document.write(`1번 : ${fruit} `); fruitFunction1(); fruitFu.. 2022. 4. 5.
JavaScript 요소의 값만 가져오기 만약 아래 사이트에서 1주 수익률 값만 가져온다고 했을 때 해당 위치의 Elements를 확인해 보면 yield 클래스로 1주 수익률이 감싸져있는 것을 확인할 수 있습니다. document.querySelector("div[class=yield]"); querySelector을 사용하여 위와 같이 yield 클래스를 가져오면 아래와 같이 해당 태그들이 나옵니다 여기서 제목은 빼고 1주 수익률만 가져올 것이기 때문에 statePt 클래스만 가져옵니다. document.querySelector("strong.statePt"); 여기서 태그를 제외한 값만 가져오기 위해서 .textContent를 사용합니다. 아래와 같이 입력해주면 document.querySelector("strong.statePt").tex.. 2022. 4. 3.
JavaScript에서 hasClass() 기능 사용하기 jQuery에서 간단하게 사용하던 hasClass if (!$('#list').hasClass('blue')) return; 클래스가 있는지 없는지 판별하는 기능을 JavaScript에서는 어떻게 쓰면 될까? classList.contains(); contains() 메서드를 사용하면 된다. if (!document.querySelector('#list').classList.contains('blue')) return; 아래 예시를 확인해 보자 #list의 li태그의 색상을 red로 변경하는데 blue라는 클래스를 가지고 있는 li만 blue로 색상을 변경한다. See the Pen classList.contains by kim oya (@ttuttu) on CodePen. 2022. 4. 1.
jQuery 이미지 위치 설정하기 $이미지.css({ left:위치값, top:위치값 }) 2022. 3. 30.
728x90
반응형
LIST