본문 바로가기

sundries

1-21 클래스 속성 $.each

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

<script src ="../js/jquery-3.2.1.js"></script>

<!--  jQuery 배열 관리 

jQuery 로 배열을 관리할 떄는 each() 메서드를 사용

-each() 메서드는 매개변수로 입력한 함수로 for~in 반복문처럼

객체나 배열의 요소를 검사하는 메서드

-each() 메서드는 다음과 같이 두 가지 형태로 사용

1. $.each(object, function(index,item){})

2. $(selector).each(function(index,item){})

첫번째 매개 변수 - index(사용된 배열의 index)

두번째 매개 변수 - item(index에 해당하는 값(value)) -->

<style>

</style>

</head>

<script>

$(function(){

var array=[

{name:"네이버", link:"http://www.naver.com"},

{name:"다음", link:"http://www.daum.net"},

{name:"구글", link:"http://www.google.com"}

];

var output='';

$.each(array, function(index,item){

output += '<a href="' + item.link +'">';

output +=   <h1>' + item.name +'</h1>';

output += '</a>';

});

document.body.innerHTML += output;

});


</script>

<body>

</body>

</html>