Javascript[クリーンアップ中]
27201 ワード
querySelector()
const tag = document.querySelector("tagname");
const id = document.querySelector("#idname");
const class = document.querySelector(".classname");
const container = document.querySelector("#title");
const matches = container.querySelectorAll("div.highlighted > p");
<div class="user-panel main">
<input name="login">
</div>
const element = document.querySelector("div.user-panel.main input[name=login]");
forEach
inputArray.forEach(function(element, index, array) {});
const body = document.querySelector("body");
const title = document.createElement("h1");
body.appendChild(title);
title.innerHTML = "Hello!";
title.style.color = "rgb(255,255,255)"; //"#FFFFFF";
const title = document.querySelector("h2");
title.innerHTML = "HOLA!";
title.style.color = "green";
function init() {
}
const init = () => {
}
Dateオブジェクト
const now = new Data(); // 객체
const now2 = new Date("yyyy-mm-dd:hh:mm:ss+0900"); // +0900:대한민국 표준시
now.getTime(); // 1970년 1월 1일 UTC(국제표준시) 00:00으로부터 지난 시간을 millisecond로 표현
now.getDate(); // 1:1일, 2:2일..
now.getDay(); // 0:월요일, 1:화요일..
now.getMonth(); // 0:1월, 1:2월..
Math.floor()
setInterval()
setInterval(func, 1000);
func関数は1秒ごとに周期的に呼び出される..valueプロパティ
document.querySelector(".className").value;
classNameのselectタグ内のオプションの値を返します.localStorage.getItem()
localStorage.getItem("keyName");
ローカルストレージでは、keyNameを使用してkeyの値を返します..selectedプロパティ
const selected = localStorage.getItem("keyName");
document.querySelector(`option[value="${selected}"]`).selected; // true or false
属性をtrueに指定します.そうでない場合falseになります.なぜJSを使うのですか?
Selector
document.getElementByClassName()[0].~
addEventListener
document.getElementById().addEventListener('click', function(){})
onclick=""
<button onclick=""></buttom>
UIの作成方法
function
Tip
JQuery
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
밑에서 부터 구현
document.getElementById('id')
document.querySelector('#id').innerHTML = "Hello";
$('#id').html("Hello");
$('#id').css('color','pink');
$('#id').attr('src','');
$('.class').on('click', function(){})
$('.class').click(function(){})
<input id=test>
$('#test').val()
田植えをする
=
1 == '1' // true
1 === '1' // false
preventDefault()
$('.form').on('submit', function(e) {
e.preventDefault();
}
var
let
const
typeof()
Array, Object
let arr = ['BMW', 520]; // 여러가지 자료형 저장
console.log(arr[0]);
let obj = { brand : "BMW", model : 520 }; // key : value
console.log(obj.brand);
console.log(obj.['brand']);
form, input, change event
$('#option1').on('change', function () {
if ($('#option1').val() == '셔츠')
$('.size-select').show();
});
Grave accent
let temp = `<option>${size[i]}</option>`;
let temp1 = "<option>S</option><option>M</option><option>L</option>"
let temp2 = `<option>S</option>
<option>M</option>
<option>L</option>`
Hoisting
test
/abc/.test('abcdef')
/[A-z]/.test('a')
/\S+@\S+\.\S+/.test('email') // 특수문자 포함 모든 문자 1개, + 붙으면 여러 문자
Event Bubbling
これは
$('.black-bg').click(function (event) {
// event.target; // 지금 실제 클릭한 요소
// event.currentTarget; // 지금 이벤트리스너가 있는 곳, $(this);
//if (event.target == event.currentTarget)
if (event.target == this)
$('.black-bg').hide();
})
Data Binding
Array.forEach(function(element){});
let size = [85, 90, 95, 100, 105, 110, 115];
size.forEach(function(e){
let temp = `<option>${e}</option>`;
$('#option2').append(temp);
});
Web開発方法
sort
let a = [20, 3, 1];
a.sort(); // 1, 20, 30
a.sort(function(a, b) { // a, b는 배열의 원소
return a - b; // 1, 3, 20
// +를 반환하면 a를 오른쪽, b를 왼쪽으로 보낸다
})
a.sort(function (a, b) {
let result = a.title > b.title;
if (result == true) {
return 1;
} else {
return -1;
}
})
filter
let a = [7, 3, 5];
let a2 = a.filter(function(e) {
return e < 6; // 3, 5
}
map
let a = [7, 3, 5];
let a2 = a.map(function(e) {
return e * 2; // 14, 6, 10
}
spread operator
let b = [3, 9, 1, 7, 5];
let c = [...b]; // 3, 9, 1, 7, 5
DOM
<script> //ERROR : p태그에 대한 DOM이 생성되기 전
document.getElementById('test').innerHTML = '안녕'
</script>
<p id="test">Hello</p>
<script>
$(document).ready(function(){
document.getElementById('test').innerHTML = '안녕'
});
</script>
<p id="test">Hello</p>
document.addEventListener('DOMContentLoaded', function() {
//DOM생성이 완료되었을 때 실행할 코드
});
AJAX
<script>
$('.btn-primary').click(function () {
$.ajax({
url: "",
type: "GET",
}).done(function (data) {
$('#id').html(data);
}).fail(function(){
}).always(function(){
});
});
</script>
GET
POST
JSON
{"brand" : "Hyundai", "model" : "Kona", "price" : 30000, "img" : "https://codingapple1.github.io/kona.jpg"}
let obj = JSON.parse(json);
let json = JSON.stringfy(obj);
Hammer.js
<script>
let img1 = document.querySelectorAll('.slide-box>img')[0];
let manager = new Hammer.Manager(img1);
manager.add(new Hammer.Pan({
threshold: 0
}));
// pan event
manager.on('pan', function (event) {
if (event.deltaX < 0) {
$('.slide-container').css('transform', `translateX(${event.deltaX}px)`);
if (event.isFinal) { // 마우스를 놓으면
$('.slide-container').addClass('transforming');
$('.slide-container').css('transform', `translateX(-100vw)`);
setTimeout(function () {
$('.slide-container').removeClass('transforming');
}, 500);
}
}
});
</script>
Reference
この問題について(Javascript[クリーンアップ中]), 我々は、より多くの情報をここで見つけました https://velog.io/@solda/Javascriptテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol