layout | title | slug | tags | |
---|---|---|---|---|
post |
2019-02-02-TIL |
190202-TIL |
|
- document.getElementById(id) : 단일 객체 반환
- document.getElementsByClassName(className)
- document.getElementsByTagName(tagName)
- document.querySelector(cssSelector) : 단일 객체 반환
- document.querySelectorAll(cssSelector)
- 속성 수정하기: Ex) imageEl.src = "w3s logo";
- 속성 삭제하기: Ex) imageEl.removeAttribute("disabled);
- 스타일 수정하기: Ex) HeadingEl.style.color = "orange";
- class 수정하기: Ex) paraEl.className = "warning"; 단 위 예시는 기존 클래스를 대체하게 될 수도 있다. 그럴 때는 +=를 사용하자. classList().add 라는 방법도 있지만 모든 브라우저에서 작동한다고 확신할 수는 없다.
- 요소의 내용을 html로 바꾸려면 innerHTML 이용
- 순수하게 텍스트만 바꾸고 싶다면 textContent 이용
- 요소 생성하기: Ex) let imgEl = document.createElement("img");
- 요소 추가하기: EX) document.body.appendChild(imgEl);
{% raw %}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF=8">
<title>DOM example</title>
</head>
<body>
<script>
let planetDiv = document.createElement("div")
planetDiv.className = "planet"
planetDiv.style.background = "orange"
document.body.appendChild(planetDiv)
let another = document.createElement("div")
another.className = "moon"
planetDiv.appendChild(another)
</script>
</body>
</html>
{% endraw %}
- 백준 알고리즘 1002번 터렛 문제를 풀었다.
- map 함수를 배웠다. map 함수는 iterator를 반환하고, lazy evaluation을 하기 때문에 list를 직접 만드는 것보다 훨씬 메모리가 절약된다는데 아직 피부에 와닿지 않는다. 좀 더 익숙해져야겠다. -_-;
- else의 중요성을 깨달았다. else는 앞선 if, elif에 해당하지 않는 모든 경우들을 처리한다. 명백한 and/or 특수한 경우만 if 및 elif로 처리하고 그 외의 경우들은 모두 else로 처리해버리는 것이 바람직하다. 그렇지 않으면 사실상 else로 한번에 묶을 수 있는 경우들에 대해서도 일일이 조건문으로 판별해야 하기 때문이다.
- 만약 중심 좌표와 반지름이 모두 다른 두 원이 있다면, 이 두 원은 한 점에서 만나거나 멀리 떨어져있거나 한 원이 다른 원 안에 들어가 있을 수 있다. 이 경우들을 제외하면 두 원은 2개의 접점을 가질 것이다. 2개의 접점을 가지는 경우에 해당하는 중심 좌표, 반지름 간의 관계를 일일이 판별하는 것보다 else로 처리하는 것이 명백하게 효율적이다.