1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<style>
    p {
        width:80px;
        height:40px;
        text-align:center;
        line-height: 40px;
        border: 1px solid #000;
        margin: 10px 0 0 0;
    }
</style>
</head>
<body>
    <div>
        <p>0</p>
        <p>1</p>
        <p>2 hello</p>
        <p>3</p>
        <p>4</p>
        <p>5</p>
        <p>6 hello</p>
        <p>7 good</p>
    </div>
 
    <script src="js/jquery.js"></script>
    <script>
        
        $('p').filter(":contains('hello')").css("background""red");  //:contains()는 특정 텍스트를 포함한 요소반환
 
        //console.log($('p').eq(0).text().indexOf('hello')); // -1
        //console.log($('p').eq(2).text().indexOf('hello')); // 2
        //console.log($('p').eq(2).text().substr(-1));       // 맨끝 문자 o반환
 
        var idx = $("p").index($("p:contains('good')"));  // 특정 텍스트를 포함한 요소 index반환     
        console.log(idx);  //7 번째 p 요소
 
    </script>    
 
</body>
</html>


반응형
Posted by 힘없는염소