프로젝트 3: Javascript를 사용하여 키보드 빌드
3628 단어 discusshtmlcssjavascript
작업 1: 웹 페이지에 모든 알파벳을 표시합니다.
작업 2: 클릭 시 브라우저 콘솔에서 문자를 인쇄합니다.
코드는 다음과 같습니다.
<html>
<body>
</body>
<script>
for (let i = 65; i <= 90; i++) {
const button = document.createElement('button');
const char = String.fromCharCode(i);
const span = document.createElement('span');
span.style.fontSize = '50px';
span.appendChild(document.createTextNode(char));
button.appendChild(span);
document.body.appendChild(button);
button.setAttribute('id', char);
button.style.padding = '30px';
button.style.margin = '10px';
button.onclick = function () { getLetter(char) };
}
function getLetter(id) {
const letter = document.getElementById(id).textContent;
console.log(letter);
}
</script>
</html>
작업1:
for (let i = 65; i <= 90; i++) {
-> 65 - 90 알파벳의 ASCII 값. 루프는 65-90 사이를 반복하고 반복을 위해 하나의 문자를 생성합니다.const button = document.createElement('button');
-> 버튼을 생성합니다.const char = String.fromCharCode(i);
-> ASCII
값에 해당하는 알파벳을 반환합니다. 예를 들어 65 -> A, 66 -> B, .... 90 -> Z.const span = document.createElement('span');
span.style.fontSize = '50px';
span.appendChild(document.createTextNode(char));
button.appendChild(span);
button
에 표시할 텍스트를 만들고 추가합니다. 더 나은 보기를 위해 fontSize
를 설정합니다.document.body.appendChild(button);
-> button
를 body
에 각각 추가합니다.button.setAttribute('id', char);
button.style.padding = '30px';
button.style.margin = '10px';
설정
id
속성은 클릭 이벤트를 실행하고 이를 문자 자체로 설정하는 데 유용합니다. 또한 더 나은 보기를 위해 일부 padding
및 margin
를 설정합니다.button.onclick = function () { getLetter(char) };
-> 각 버튼이 트리거onclick
기능을 수행하도록 설정getLetter
합니다.function getLetter(id) {
const letter = document.getElementById(id).textContent;
console.log(letter);
}
우리는 그것의
button
로 id
를 얻고 기본적으로 당신이 클릭한 글자인 그것의 textContent
를 캡처하고 있습니다.다음으로
console
로 인쇄합니다.결과는 다음과 같습니다.
오늘은 여기까지입니다. 내일 더 발전하도록 노력하겠습니다.
감사합니다😊 즐겁게 읽어주세요!.
💎 귀하의 응답을 보고 싶습니다.
Like - 여기에 도달했습니다. 나는 좋아할 자격이 있다고 생각합니다.
의견 - 함께 배울 수 있습니다.
공유 - 다른 사람들도 이 리소스를 유용하게 사용할 수 있습니다.
구독/팔로우 - 내 매일 기사를 최신 상태로 유지합니다.
격려해 주세요 - You can buy me a Coffee
더 논의합시다.
또는 기재
추가 업데이트:
Reference
이 문제에 관하여(프로젝트 3: Javascript를 사용하여 키보드 빌드), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/urstrulyvishwak/project-3-build-keyboard-using-javascript-59fg텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)