JavaScript/jQuery

【jQuery/Selector #52】:only-of-type Selector

1Q74 2023. 3. 15. 02:51

1. Description

jQuery("only-of-type")
형제 Element가 없고, 태그명이 같고, 한 개만 존재하는 Element를 찾는다.

2. Example

#1버튼을 클릭하면 <td>태그 내에 있는 Element들 중에서, 태그명을 기준으로하여 한 개 뿐인 Element를 찾아 배경색을 변경한다. 버튼을 클릭할 때마다 스타일이 토글된다.


[1q74.tistory.com] javascript/jquery/selector/only-of-type
child#1-1
child#1-2
child#1-3
child#2-1
child#2-2 child#2-3
  • child#3-1
  • child#3-2
    child#3-3
  • child#4-1
  • child#4-2
  • child#4-3





    [HTML Code]
    [//HTML Code]

    3. Code

    더보기
    <!-- ---------------------------------------------------------
      --
      -- Author: 1q74.tistory.com
      --
      --------------------------------------------------------- -->
    <!doctype html>
    <html lang="ko">
    <head>
    <meta charset="utf-8">
    <title>[1q74.tistory.com] javascript/jquery/selector/only-of-type</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
    </head>
    <body>
    
    	<div name="_1q74-example-root">
    		<div name="_1q74-example-1">
    			<table border="1">
    				<tr>
    					<td>
    						<h5>child#1-1</h5>
    						<h5>child#1-2</h5>
    						<span>child#1-3</span>
    					</td>
    				</tr>
    				<tr>
    					<td>
    						<div>child#2-1</div>
    						<span>child#2-2</span>
    						<span>child#2-3</span>
    					</td>
    				</tr>
    				<tr>
    					<td>
    						<li>child#3-1</li>
    						<div>child#3-2</div>
    						<div>child#3-3</div>
    					</td>
    				</tr>
    				<tr>
    					<td>
    						<li>child#4-1</li>
    						<li>child#4-2</li>
    						<h5>child#4-3</h5>
    					</td>
    				</tr>
    			</table>
    		</div>
    
    		<div name="_1q74-example-bottom">
    			<br/>
    			<button name="btn-nth-of-type-1">#1. $("[name='_1q74-example-1'] span:only-of-type")</button><br/>
    			<button name="btn-nth-of-type-2">#2. $("[name='_1q74-example-1'] div:only-of-type")</button><br/>
    			<button name="btn-nth-of-type-3">#3. $("[name='_1q74-example-1'] li:only-of-type")</button><br/>
    			<button name="btn-nth-of-type-4">#4. $("[name='_1q74-example-1'] h5:only-of-type")</button>
    		</div>
    	</div>
    
    	<hr/>
    	<h5>[HTML Code]</h5>
    	<div name="_1q74-source">
    	</div>
    	<h5>[//HTML Code]</h5>
    
    <script>
    var btnContainer = $("[name='_1q74-example-bottom']");
    var btnChild1 = btnContainer.find("[name='btn-nth-of-type-1']");
    var btnChild2 = btnContainer.find("[name='btn-nth-of-type-2']");
    var btnChild3 = btnContainer.find("[name='btn-nth-of-type-3']");
    var btnChild4 = btnContainer.find("[name='btn-nth-of-type-4']");
    
    var child1 = $("[name='_1q74-example-1'] span:only-of-type");
    var child2 = $("[name='_1q74-example-1'] div:only-of-type");
    var child3 = $("[name='_1q74-example-1'] li:only-of-type");
    var child4 = $("[name='_1q74-example-1'] h5:only-of-type");
    
    btnChild1.click(function() {
    	if(!child1.is("[style]")) {
    		child1.css("background", "#AE5907");
    	} else {
    		child1.removeAttr("style");
    	}
    });
    
    btnChild2.click(function() {
    	if(!child2.is("[style]")) {
    		child2.css("background", "#07AE3A");
    	} else {
    		child2.removeAttr("style");
    	}
    });
    
    btnChild3.click(function() {
    	if(!child3.is("[style]")) {
    		child3.css("background", "#02764C");
    	} else {
    		child3.removeAttr("style");
    	}
    });
    
    btnChild4.click(function() {
    	if(!child4.is("[style]")) {
    		child4.css("background", "#95A8E4");
    	} else {
    		child4.removeAttr("style");
    	}
    });
    
    // ------------------------------------------------------
    // 소스출력
    // ------------------------------------------------------
    var textSource = $("[name='_1q74-source']");
    var exampleHtml = $("[name='_1q74-example-root']").html();
    var specialCharLines = textSource.text(exampleHtml).html().split("\n");
    
    textSource.html(specialCharLines.join("<br/>").replaceAll("\t", "&nbsp;&nbsp;"));
    </script>
    <style>
    [name="_1q74-example-root"] {
    	min-height: 170px;
    }
    
    [name="_1q74-example-1"] {
    	min-height: 140px;
    }
    
    [name="_1q74-example-root"] [type="image"]{
    	width: 100px;
    }
    
    [name="_1q74-example-1"] td {
    	min-width: 100px;
    	min-height: 30px;
    }
    
    [name|="_1q74-example"] button
    ,[name="_1q74-source"] button {
    	background: lightgray;
    	border: 5px outset;
    	min-height: 35px;
    }
    
    [name|="_1q74-example"] button:active
    ,[name="_1q74-source"] button:active {
    	border: 2px inset;
    	min-height: 35px;
    }
    </style>
    
    </body>
    </html>

    4. File

    [javascript][jquery][selector]only-of-type.html
    0.00MB


    5. See also

    useversal-selector


    6. Reference

    https://api.jquery.com/only-of-type-selector/