티스토리 뷰

1. 객체 추가

html() 태그를 활용한 문서 객체 내부 추가


2. 객체 추가 - 예제1

 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
<script src="https://code.jquery.com/jquery-1.11.3.js" type="text/javascript"></script>
 
<script>
$(document).ready(function () 
{
    $('div').html('<h1>test1</h1>');
});
</script>
 
<body>
    <div></div>
    <div></div>
</body>





 결과





 결과코드


1
2
3
4
<body>
    <div><h1>test1</h1></div>
    <div><h1>test1</h1></div>
</body>



3. 객체추가 - 예제2

 코드


1
2
3
4
5
6
7
8
9
10
<script src="https://code.jquery.com/jquery-1.11.3.js" type="text/javascript"></script>
 
<script>
$(document).ready(function () 
{
    $('div').html(function(index){
        return '<h1>test'+index+'</h1>';
        });
});
</script>





 결과







 결과코드


1
2
3
4
<body>
    <div><h1>test0</h1></div>
    <div><h1>test1</h1></div>
</body>


4. 객체추가 - 예제3

 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script src="https://code.jquery.com/jquery-1.11.3.js" type="text/javascript"></script>
 
<script>
$(document).ready(function () 
{
    $('div').html(function(index,html){
        return '<h1>ID -  '+html+'</h1>';
        });
});
</script>
 
<body>
    <div>test1</div>
    <div>test2</div>
</body>





 결과







 결과코드


1
2
3
4
<body>
    <div><h1>ID - test1</h1></div>
    <div><h1>ID - test2</h1></div>
</body>



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday