javascript - 隐藏符号后的内容

标签 javascript string

我有一个列表,想要使用 javascript 隐藏所有列表项上破折号后的所有内容

<ul id="list">
 <li class="item">
  this is the main stuff - this is the rest
 </li>
 <li class="item">
  this is the main stuff - this is the rest
 </li>
 <li class="item">
  this is the main stuff - this is the rest
 </li>
</ul>

到目前为止,我遇到了这段代码,但我不确定如何实现它,以便它对某个列表 idclass 中的所有内容产生影响。

var s = 'test:string'; 
s.split(':')[0];

我该怎么做?

最佳答案

这样就可以了。希望能帮助到你。您可以使用查询选择器全部来选择具有(id)及其子项的特定列表。由于您没有html标签,因此您必须遵循一些类似的方法,否则只需将文本包裹在 span 中并隐藏即可。

function hide(id,cl) {
  let data = document.querySelectorAll(`#${id} >.${cl}`);

  data.forEach(ele => {

    ele.textContent = ele.textContent.split("-")[0]
  })


}
// More general solution just pass the id and class 
hide("list","item")
<ul id="list">
  <li class="item">
    this is the main stuff - this is the rest
  </li>
  <li class="item">
    this is the main stuff - this is the rest
  </li>
  <li class="item">
    this is the main stuff - this is the rest
  </li>
</ul>

关于javascript - 隐藏符号后的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57866098/

相关文章:

javascript - Jquery如何向上/向下移动X行所选行

java - HashMap<String,Double> 的格式错误的二进制序列化

java - 无法使用java中的扫描器扫描字符串数组的所有元素

C 更改传递给函数的字符串不起作用

string - Delphi:如何从字符串中获取*真实的* ASCII 代码

javascript - 授予对 s3 目录的 Web 访问权限的最佳方法

javascript - Express js 中 View 之间传递数据

javascript - JavaScript 中键值对数组的求和值

javascript - Jasminejs - 测试回调

c - 用C语言编写简单的字符表