简体   繁体   English

隐藏/删除 div 中的文本

[英]hide/remove text in div

I have this div and I need to remove certain parts if a condition is fulfilled.我有这个 div,如果满足条件,我需要删除某些部分。

I don't have access to the HTML and CSS files of the project, so I can only work with JavaScript here.我无权访问项目的 HTML 和 CSS 文件,因此我只能在这里使用 JavaScript。

For example I want to delete everything from "Starting date" to "Starting time"例如,我想删除从“开始日期”到“开始时间”的所有内容

<div class="timer">
Starting date
<select>
  <option value="1"></option>
  <option value="2"></option>
</select>
Starting time
<select>
...

How can I remove the entire content of the div from a certain point to another certain point?如何将 div 的全部内容从某个点删除到另一个点?

This work after making the nodelist static使节点列表静态化后的这项工作

 let hideDate = true; // your condition const startText = "Starting date", endText = "Starting time"; if (hideDate) { let i = 0; const nodes = [...document.querySelector(".timer").childNodes]; // make list static for (; i < nodes.length; i++) { if (nodes[i].nodeType === 3 && nodes[i].textContent.trim() === startText) break; } for (; i < nodes.length; i++) { if (nodes[i].nodeType === 3 && nodes[i].textContent.trim() === endText) break; nodes[i].remove(); } }
 <div class="timer"> Starting date <select> <option value="1"></option> <option value="2"></option> </select> Starting time <select> <option value="1"></option> <option value="2"></option> </select> </div>

i think you can do something like this:我认为你可以做这样的事情:

you have to select this element from javaScript for example:例如,您必须从 javaScript 中选择此元素:

var elementsContainer = document.getElementsByClassName/TagNames('element class or element tag name');

this will return you a list of elements, so you need to get one of them, like this:这将返回一个元素列表,因此您需要获取其中一个,如下所示:

elementsContainer = elementsContainer[0]

this will give you one element, so after this, you can delete its content like this这会给你一个元素,所以在这之后,你可以像这样删除它的内容

elementsContainer.innerHTML/innerText = '';

you can choose one of this property innerHTML or innerText according to which you need您可以根据需要选择此属性innerHTML或innerText之一

Well for your problem.好为你的问题。 I would suggest to have a look at the concepts of jquery remove() , and jquery empty() .我建议看看jquery remove()jquery empty() here are some working test demos of these two methods.这是这两种方法的一些工作测试演示。

<script>
$(document).ready(function(){
    $(".timer select").remove();
});
</script>

The above code will completely remove the elements inside the div element.上面的代码将彻底删除div元素内的元素。 see here Jquery Remove Wroking Example在这里看到Jquery 删除工作示例

<script>
$(document).ready(function(){
    $(".timer").empty();
});
</script>

This method removes not only child (and other descendant) elements, but also any text within the set of matched elements Jquery Empty Working Example此方法不仅删除子(和其他后代)元素,还删除匹配元素集中的任何文本Jquery Empty Working Example

Similar to .empty(), the .remove() method takes elements out of the DOM.与 .empty() 类似,.remove() 方法从 DOM 中取出元素。 Use .remove() when you want to remove the element itself, as well as everything inside it.当您想要删除元素本身以及其中的所有内容时,请使用 .remove() 。

If you want to do it with core javascript then have a look at it.如果你想用核心 javascript 来做,那么看看它。 Jquery Remove method using core JS使用核心 JS 的 Jquery Remove 方法

 let timer = document.querySelector('.timer') const from = 'Starting date' const to = 'Starting time' let html = timer.innerHTML timer.innerHTML = html.slice(0, html.search(from)) + html.slice(html.search(to))
 <div class="timer"> Starting date <select> <option value="1">1</option> <option value="2">2</option> </select> Starting time <select> <option value="1">1</option> <option value="2">2</option> </select> </div>

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

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