JavaScript/jQuery

【jQuery/Selector #18】Element Selector (“element”)

1Q74 2023. 2. 22. 23:20

1. Description

해당 태그 이름으로 된 모든 Element들을 찾는다.


2. Example

#1버튼을 클릭하면 <h1>, <h2>, <h3>태그를 찾아서 텍스트 중간에 'Great'을 삽입한다. 해당 버튼을 클릭할 때마다 문자열 삽입과 삭제가 토글된다.


[1q74.tistory.com] javascript/jquery/selector/element

My Stories

  • TStory
  • Story Challenge
  • UStory
  • WStory
  • Story Maker
  • Love Story
  • Your Story

  • Happy Ending Story
  • Their Story

  • Happy Ending Story Too


  • [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/element</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">
    			<h1>My Stories</h1>
    			<li value="tstory">TStory</li>
    			<li value="story challenge">Story Challenge</li>
    			<li value="ustory">UStory</li>
    			<li value="wstory">WStory</li>
    			<li value="story maker">Story Maker</li>
    			<li value="love story" style="color:#567452">Love Story</li>
    
    			<h2>Your Story</h2>
    			<li value="happyendingstory">Happy Ending Story</li>
    
    			<h3>Their Story</h3>
    			<li value="happyendingstorytoo">Happy Ending Story Too</li>
    		</div>
    
    		<div name="_1q74-example-bottom">
    			<br/>
    			<button name="btn-find">#1. $("[name='_1q74-example-1']").find("> h1, h2, h3")</button>
    		</div>
    	</div>
    
    	<hr/>
    	<h5>[HTML Code]</h5>
    	<div name="_1q74-source">
    	</div>
    	<h5>[//HTML Code]</h5>
    
    <script>
    const btnFind = $("[name='btn-find']");
    
    btnFind.click(function() {
    	const hTag = $("[name='_1q74-example-1']").find("> h1, h2, h3");
    
    	hTag.each(function(index, el) {
    		const element = $(el);
    		const isAddedText = element.filter(":contains('Great')").length > 0;
    
    		if(!isAddedText) {
    			element.html(
    				element.text().replace(" ", " <span style='color:green;'>Great</span> ")
    			);
    		} else {
    			element.find("span").remove();
    		}
    	});
    });
    
    // ------------------------------------------------------
    // 소스출력
    // ------------------------------------------------------
    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"] button
    ,[name="_1q74-source"] button {
    	background: lightgray;
    	border: 5px outset;
    	min-height: 30px;
    }
    
    [name|="_1q74-example"] button:active
    ,[name="_1q74-source"] button:active {
    	border: 2px inset;
    	min-height: 30px;
    }
    </style>
    
    </body>
    </html>

    4. File

    [javascript][jquery][selector]element.html
    0.00MB


    5. Reference

    https://api.jquery.com/element-selector/