$("선택자").attr("엘리먼트 속성");
엘리먼트 속성의 값을 불러온다.
ex)
$('img').attr("src");$("선택자").attr("엘리먼트 속성","속성값");
엘리먼트 속성에 속성값을 넣는다.
ex)
$('img').attr("src","images/img_02.jpg");예제1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ko" xml:lang="ko">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title> new document </title>
<style type="text/css">
.a{outline:5px solid red}
p span{display:inline-block;margin-left:2px;width:80px;height:30px;background:#aaa;line-height:30px;text-align:center;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
$('img').attr("src","images/img_02.jpg");
});
});
</script>
</head>
<body>
<img src="images/img_01.jpg" alt="신세계" id="a" class="b" />
<button>OK</button>
</body>
</html>미리보기
예제2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ko" xml:lang="ko">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title> new document </title>
<style type="text/css">
.a{outline:5px solid red}
p span{display:inline-block;margin-left:2px;width:80px;height:30px;background:#aaa;line-height:30px;text-align:center;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
$('img').attr("class","a");
});
});
</script>
</head>
<body>
<img src="images/img_01.jpg" alt="신세계" class="b" />
<button>OK</button>
</body>
</html>미리보기
예제3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ko" xml:lang="ko">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title> new document </title>
<style type="text/css">
.a{outline:5px solid red}
p span{display:inline-block;margin-left:2px;width:80px;height:30px;background:#aaa;line-height:30px;text-align:center;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
var str=$('img').attr("class");//img의 class를 가져옴
alert(str);
var str1=$('img').attr("alt");//img의 alt를 가져옴
alert(str1);
});
});
</script>
</head>
<body>
<img src="images/img_01.jpg" alt="신세계" class="b" />
<button>OK</button>
</body>
</html>
미리보기
예제4
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ko" xml:lang="ko">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title> new document </title>
<style type="text/css">
.a{outline:5px solid red}
p span{display:inline-block;margin-left:2px;width:80px;height:30px;background:#aaa;line-height:30px;text-align:center;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('img').click(function(){
var str=$('#a1').attr("src");
var str1=$('#a1').attr("alt");
$('#b1').attr("src",str);
$('#b1').attr("alt",str1);
});
});
</script>
</head>
<body>
<img src="images/img_01.jpg" alt="신세계" id="a1" />
<img src="" alt="" id="b1" />
</body>
</html>
미리보기
예제5
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ko" xml:lang="ko">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title> new document </title>
<style type="text/css">
.a{outline:5px solid red}
p span{display:inline-block;margin-left:2px;width:80px;height:30px;background:#aaa;line-height:30px;text-align:center;cursor:pointer;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#t1').click(function(){
var url1=$('#t1').attr("title");
$('a').attr("href",url1);
});
$('#t2').click(function(){
var url2=$('#t2').attr("title");
$('a').attr("href",url2);
});
$('#t3').click(function(){
var url3=$('#t3').attr("title");
$('a').attr("href",url3);
});
});
</script>
</head>
<body>
<p><span title="http://www.naver.com" id="t1">네이버</span><span title="http://www.daum.net" id="t2">다음</span><span title="http://www.nate.com" id="t3">네이트</span></p>
<p><a href="">링크</a></p>
</body>
</html>
if문을 써서 하는 방법
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ko" xml:lang="ko">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title> new document </title>
<style type="text/css">
.a{outline:5px solid red}
p span{display:inline-block;margin-left:2px;width:80px;height:30px;background:#aaa;line-height:30px;text-align:center;cursor:pointer;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
if($(this).attr("id"=="bt1")){
$('a').attr("href","http://www.naver.com");
}else if($(this).attr("id"=="bt2")){
$('a').attr("href","http://www.daum.net");
}else{
$('a').attr("href","http://www.nate.com");
}
});
});
</script>
</head>
<body>
<button id="bt1">네이버</button><button id="bt2">다음</button><button id="bt3">네이트</button>
<a href="#">링크</a>
</body>
</html>미리보기
댓글 0
댓글 쓰기 권한이 없습니다.
| 41 | 이클립스 jQuery 플러그인 | 2014.10.27 |
| 40 | 140813_[jquery] 이벤트 | 2014.09.23 |
| 39 | 140813_[jquery] 마우스 이벤트 | 2014.09.23 |
| 38 | 140813_[jquery] hover | 2014.09.23 |
| 37 | 140813_[jquery] mouseover | 2014.09.23 |
| 36 | 140813_[jquery] setInterval | 2014.09.23 |
| 35 | 140812_[jquery] show(slide) | 2014.09.23 |
| 34 | 140811_[jquery] animate | 2014.09.23 |
| 33 | 140811_[jquery] 왼쪽 slide menu | 2014.09.23 |
| 32 | 140808_[jquery] slideUp/slideDown | 2014.09.23 |
| 31 | 140808_[jquery] hide/show | 2014.09.23 |
| 30 | 140808_[jquery] fadeout | 2014.09.23 |
| 29 | 140808_[jquery] val | 2014.09.22 |
| 28 | 140807_[jquery] html | 2014.09.22 |
| 27 | 140807_[jquery] text,attr | 2014.09.22 |
| 26 | 140806_[jquery] text | 2014.09.22 |
| > | 140806 -[jquery] attr | 2014.09.22 |
| 24 | 140805 -[jquery] css,selector | 2014.09.18 |
| 23 | jqury 소스 링크 | 2014.08.26 |
| 22 | 140725_1 | 2014.07.25 |