$("요소").css("속성이름", "값");
let 변수 = $("요소").css("css속성이름"):
$("요소").css({
"속성":"값",
"속성":"값",
"속성":"값"
});
let 변수 = $("요소").hasClass("클래스이름");
$("요소").addClass("클래스이름");
$("요소").removeClass("클래스이름");
if( $("요소").hasClass("클래스이름") ){
$("요소").removeClass("클래스이름");
} else {
$("요소").addClass("클래스이름");
}
-> 구문축약 가능
$("요소").toggleClass("클래스이름");
1)
$(".btn").not($("#mybtn")).css(...);
-> #mybtn을 제외한 .btn에 css적용
2)
$(".btn").not(".btn:eq(2)").css(...);
-> .btn에 2번째 항목을 제외한 .btn에 css적용
<style>
/* 테이블의 모든 칸에 동일한 크기와 정렬 지정 */
td { width: 100px; height: 100px; text-align: center; }
</style>
</head>
<body>
<table border="1">
<tr>
<td>1번</td> <td>2번</td> <td>3번</td>
</tr>
<tr>
<td>4번</td> <td>5번</td> <td>6번</td>
</tr>
<tr>
<td>7번</td> <td>8번</td> <td>9번</td>
</tr>
</table>
$("td:eq(4)").next().css("background","yellow"); // 6번
$("td").eq(4).prev().css("background","SlateBlue"); // 4번
$("td:eq(4)").parent().css("color","blue"); // td(4)의 부모인 tr이 선택. 4, 5, 6번
$("td:eq(4)").parent().next().css("color","red"); // tr의 next이므로 7, 8, 9번
$("td:eq(4)").parent().prev().css("color", "MediumSeaGreen"); // 1, 2, 3번
$("td:eq(4)").parent().next().children().css("text-decoration", "underline"); // 7, 8, 9
$("td:eq(4)").parent().next().children().eq(1).css("font-weight", "bold"); // 8
$("td:eq(4)").css("background","tomato"); // 5번
$("td").eq(2).css("background","powderblue"); // 3번
<body>
<ul id="first">
<li class="foo">list item1</li>
<li>list item2</li>
<li class="bar">list item3</li>
</ul>
<ul id="second">
<li class="foo">list item1</li>
<li>list item2</li>
<li class="bar">list item3</li>
</ul>
<script>
$("#first").find(".foo").css("background-color", "tomato").end()
.find('.bar').css("background-color", "orange");
</script>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<style>
p{background: tomato;}
</style>
</head>
<body>
<p id="user_p">jQuery는 : </p>
<script>
$("p") .append("<strong>재미있다</strong>");
// document.getElementById("user_p").innerHTML = document.getElementById("user_p").textContent + "<strong>재미있다</strong>";
</script>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
</head>
<body>
<p> jQuery는 : </p>
<script>
$("p").after("<strong>재미있다</strong>");
</script>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<style>
div {
border:2px blue solid;
margin:2px;
padding:2px;
}
p {
background:yellow;
margin:2px;
padding:2px;
}
strong {
color:red;
}
</style>
</head>
<body>
<span>Span text</span>
<strong>What about me?</strong>
<span>Another one</span>
<script>
$("span").wrap("<div><div><p><em><b> </b></p></div></div>");
</script>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<style>
p{background: tomato; margin: 6px 0;}
</style>
</head>
<body>
<p>Hello</p>
how are
<p>you?</p>
<button>call remove()</button>
<script>
$("button").click(function(){
$("p").remove();
});
</script>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<style>
p{background: powderblue; margin: 6px 0;}
</style>
</head>
<body>
<p>Hello</p>
<p>javascript</p>
<p>World</p>
<script>
$("<b>jQuery</b>").replaceAll("p");
</script>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<style>
p {
margin: 4px;
font-size:16px;
font-weight:bolder;
cursor:pointer;
}
.blue {
color:blue;
}
.highlight {
background:powderblue;
}
</style>
</head>
<body>
<p class="blue">click to toggle</p>
<p class="blue highlight">highlight</p>
<p class="blue">on these</p>
<p class="blue">paragraphs</p>
<script>
$("p").click(function(){
$(this).toggleClass("highlight");
});
</script>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<style>
p{color: green; margin: 8px;}
</style>
</head>
<body>
<input type="text" value="some text"/>
<p></p>
<script>
$("input") .keyup(function(){
let value = $(this).val();
$("p").text(value);
})
</script>
</body>
좋은 글 감사합니다. 자주 방문할게요 :)