简体   繁体   English

从 jQuery 中删除 HTML 元素

[英]Remove an HTML element from jQuery

I have the following basic HTML:我有以下基本 HTML:

<select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option></select>

<h1 id="firt">First</h1>
<h2 id="second">Second</h2>

When the value SECOND is selected, a jQuery function should remove <h2 id="second">Second</h2> , so I made the following function:当该值SECOND被选择时,jQuery函数应该删除<h2 id="second">Second</h2>所以提出了以下功能:

$(document).on('change', '.selector', function() {
    tst = $(this).val()
    if (tst == 'second'){
        $('#second').remove()
    }
});

It works without any problem, the trouble is that my current function will remove that element definitively, instead, if the value FIRST is selected after SECOND , my html should go back to the initial state.它没有任何问题,问题是我当前的函数将明确删除该元素,相反,如果在SECOND之后选择了FIRST值,我的 html 应该回到初始状态。 How can I handle it?我该如何处理? Should I use another function instead of remove() here?我应该在这里使用另一个函数而不是remove()吗?

You can match the current value with id of the element to be shown:您可以将当前值与要显示的元素的id匹配:

 $(document).on('change', '.selector', function() { var tst = $(this).val(); $('#first, #second').hide(); $(`[id=${tst}]`).show() }); $('.selector').trigger('change'); // trigger the event on page load to show the element by matching the value
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option></select> <h1 id="first">First</h1> <h2 id="second">Second</h2>

you should use hide when the selector is second and show other than selector second您应该在选择器为第二个时使用隐藏并显示选择器第二个以外

 $(document).on('change', '.selector', function() { tst = $(this).val() if (tst == 'second'){ $('#second').hide(); } else{ $('#second').show(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option> <option value="third">Third</option> </select> <h1 id="firt">First</h1> <h2 id="second">Second</h2>

you can show and hide the element instead of removing it.您可以显示和隐藏元素而不是删除它。 add common class as heading to both header and show it by default.添加公共类作为heading到两个标题并默认显示它。

see below code看下面的代码

NOTE: correct the spelling of header with id="first"注意:用 id="first" 更正标题的拼写

 $(function(){ $(document).on('change', '.selector', function() { $('.heading').show(); $('#' + $(this).val()).hide() }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option></select> <h1 class="heading" id="first">First</h1> <h2 class="heading" id="second">Second</h2>

What you can do is play with the css display: none rule:您可以做的是使用 css display: none规则:

 $(document).on('change', '.selector', function() { tst = $(this).val() if (tst == 'second'){ $('#second').css({'display': 'none'}) } else { $('#second').css({'display': 'inline'}) } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option> </select> <h1 id="firt">First</h1> <h2 id="second">Second</h2>

It will make the element disappear without removing it completely from the DOM.它将使元素消失而不将其从 DOM 中完全删除。

As @Umair mentioned, use show / hide for this.正如@Umair 提到的,为此使用show / hide

 $(document).on('change', '.selector', function() { tst = $(this).val() $("#second, #first").show(); if (tst == 'second'){ $('#second').hide() } else { $('#first').hide() } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option></select> <h1 id="first">First</h1> <h2 id="second">Second</h2>

Jquery show hide will be the better way to do Jquery show hide 将是更好的方法

 $(document).on('change', '.selector', function() { tst = $(this).val() if(tst == 'second') $('#second').hide(); else $('#second').show(); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option></select> <h1 id="firt">First</h1> <h2 id="second">Second</h2>

Using change method we can manage hide/show in jQuery.divText set instance for selector according div id will hide & show使用更改方法,我们可以根据 div id 在 jQuery.divText 设置实例中管理隐藏/显示选择器将隐藏和显示

$(".selector").change(function() {
  let divText = $(this).val();

  if (divText === 'second') {
    $('#second').hide();
  } else {
    $('#second').show();
  };
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM