Javascript ul li 삭제 - Javascript ul li sagje

1. remove()

해당 selector를 삭제하는 함수이다.

<ul class="cont s_list">

<li>

<a href="view.aspx" class="img_cont">

<div class="b_img">

<img src="http://img.nocutnews.co.kr/nocut/v2/timeline/dog02.jpeg" alt="이미지" />

</div>

<div class="text">

<p>'박 전 대통령'향후 검찰수사,전망</p>

<div class="text_cont">

<span class="date">2017.08.31 09:00</span>

<span class="vnum">11</span>

</div>

</div>

</a>

</li>

</ul>

cs

위 HTML을 예제로 설명한다.

1

$(".cont s_list").remove();

cs

실행하면, ul 태그 뿐만 아니라 ul 태그 안에 있는 li, a, div 등 모든 태그의 내용이 삭제된다.

2. empty()

해당 selector 안에 있는 태그 및 내용을 삭제하는 함수이다.

1

$(".cont s_list").empty();

cs

실행하면, ul 태그는 존재하고 안에 있는 태그 및 내용이 삭제된다.

이 영상에서는 자바스크립트에서 UL에서 선택한 LI 삽입 업데이트 삭제를 안내합니다.



서로 옆에 있는 div
  • 새로 삽입 |_+_| A에게 |_+_| .
  • 선택 표시 |_+_| 목록에서 입력 텍스트로 .
  • 선택한 항목 업데이트 |_+_| 입력 텍스트를 사용하여 목록에서 .
  • 선택한 항목 제거 |_+_| 목록에서 .

JS 및 Netbeans Editor의 3 버튼 클릭 이벤트에서.

구독하다 : https://www.youtube.com/channel/UCS3W5vFugqi6QcsoAIHcMpw


프로젝트 소스 코드:

초보자 웹 개발 프로젝트
  • #js #자바스크립트



    www.youtube.com

    Javascript를 사용하여 선택한 LI를 추가, 편집, 제거하는 방법 [소스 코드 포함]

    이 영상에서는 자바스크립트에서 UL에서 선택한 LI 삽입 업데이트 삭제를 안내합니다.

    Without having to reach for jQuery, Vue, React or another fancy library you can easily remove the contents of a HTML list with just plain JavaScript. Lets assume that we have the following list in HTML.

    <ul id="example">
      <li>...</li>
      <li>...</li>
    </ul>
    

    There is a couple ways we can go about removing the li elements, but we need to start with a selector to store our list in a variable. Since the list has the ID example we can use that to find the list in the DOM.

    const list = document.getElementById("example");
    

    Maybe the easiest way to empty the list is by setting its innerHTML. This will erase all the contents of the list.

    list.innerHTML = "";
    

    This will leave you with an empty list, as follows:

    <ul id="example"></ul>
    

    While effective, this approach is somewhat rigorous too. As mentioned before, it will erase all the contents of the list. So if you had something else in your list, like a div for example, it is removed as well.

    Of course there is a way to just remove the li elements too. With a for loop we can remove just remove the li elements. We use a different selector here to get all the li's of the list instead of the list itself.

    var listElements = document.querySelectorAll("#example li");
    
    for (var i = 0; (li = listElements[i]); i++) {
      li.parentNode.removeChild(li);
    }
    
    • javascript