특정 영역을 제외한 부분을 클릭했을 때 이벤트를 발생시켜야 할 때가 있다. 그때는 다음과 같이 하면 된다.


<html>
	<head>
		<meta charset="utf-8">
		<script   src="https://code.jquery.com/jquery-2.2.4.min.js"   integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="   crossorigin="anonymous"></script>
	</head>

<body>
	<div class="area" style="width:200px; height:200px; background-color: #4574bb; margin: auto;">
	</div>
</body>
</html>

<script>
$('html').click(function(e) {   
	if(!$(e.target).hasClass("area")) {
		alert('영역 밖입니다.');
	}
});    
</script>
$('html').click(function(e) {   
	if(!$(e.target).hasClass("area")) {
		alert('영역 밖입니다.');
	}
});    

즉 특정 영역에 class명을 부여하고 클릭한 곳에 그 클래스명이 없으면 이벤트를 발생시키는 로직입니다.

<ul>  
    <li value="1">One</li>
    <li value="2">Two</li>
    <li value="3">Three</li>
    <li value="4">Four</li>
    <li value="5">Five</li>
</ul>

를 다음과 같이 배열로 만드는 방법은 여러가지 겠지만 간단히 map 함수를 이용할 수 있다.

[1, 2, 3, 4, 5]
var arr = $.map($("li"), function(item) {
    return $(item).attr("value");
});
<div class="parent">
	First
	<div class="child">Child</div>
	Last
</div>

다음과 같을 때 First 를 Second로 바꾸고 싶을 때 어떻게 하면 될까?!

$(".parent").contents()

를 하면 다음과 같이 나온다.

["First", <div class="child">Child</div>, "Last"]
$(".parent").contents()[0].textContent = "Second"

하면 First 값이 Second로 바뀐다.

브라우저에 element가 보이는지 체크하려면 다음과 같이 하면 된다.

$(element).is(":visible")

submit 하고 나서 동적으로 데이터를 삽입하여 변수를 서버로 전송할 때 동적으로 Param을 넘겨줄 때 다음과 같이 사용하면 된다.

선택자 "#form1" 은 <form> 태그의 ID 값을 임의로 넣은 것이다.


var input = $("<input>")
               .attr("type", "hidden")
               .attr("name", "mydata").val("bla");
$('#form1').append($(input));
    


+ Recent posts