javascript 或 css : How to Hide any Number followed by dot Prefix "1.text", "2.text".. ."30.text"来自标签内的文本

标签 javascript html css hide prefix

我想用 javascript 或 css 隐藏标签内文本的所有(数字后跟点)前缀。 隐藏“1.”、“2..”...“30.”来自标签文本的“1.text”、“2.text”...“30.text”。

HTML
<li>
<label class="layered_nocolor">
1.alpha
</label>
</li>
<li>
<label class="layered_nocolor">
2.beta
</label>
</li>
<li>
<label class="layered_nocolor">
30.gama
</label>
</li>

我希望我的页面显示没有前缀的文本

alpha
beta
gama

最佳答案

您将无法使用 CSS OR Javascript 隐藏任何内容。你将需要他们两个。您将不得不使用 CSS 来隐藏标签和 Javascript 来动态地改变标签的样式。首先,创建一个名为 hide 的 CSS 类:

.hide{
   display: none;
 }

然后,在您的 HTML 中,您将添加此脚本:

// Gets all your HTML element with the class "layered_nocolor".
var labels = document.getElementsByClassName("layered_nocolor");
// The regex pattern you want to hide. It means "one or more digit before a dot".
var pattern = "(/d+\.)";
// Iterates all the elements you got from before. 
for(int i = 0; i < labels.length; i++){
  // If your label match the regex pattern you change the class hiding it.
  if(labels[i].innerHTML.match(pattern) != null){
    labels[i].class = "layered_nocolor hide";
  }
}

编辑

经过您的解释,这是您要查找的代码:

// Gets all your HTML element with the class "layered_nocolor".
var labels = document.getElementsByClassName("layered_nocolor");
// The regex pattern you want to hide. It means "one or more digit before a dot".
var pattern = "(/d+\.)";
// Iterates all the elements you got from before. 
for(int i = 0; i < labels.length; i++){
  // If your label match the regex pattern you replace the text with "" to remove it.
 (labels[i].innerHTML.replace(pattern, "");
}

编辑 2

如果您正在使用 jQuery,这是最好的方法:

$(".layered_nocolor").html(function(i, oldhtml){return oldhtml.replace(/\\d+\\./, "");});

双“\”很重要,因为如果只插入一次,它将用作转义字符而不是文字。

关于javascript 或 css : How to Hide any Number followed by dot Prefix "1.text", "2.text".. ."30.text"来自标签内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36945621/

相关文章:

javascript - 无法使用post请求nodejs重定向

javascript - 添加事件监听错误

jquery - 视频背景大小问题

html - 网格元素内的 Div?

html - 在 Firefox 中水平显示而不是垂直显示的下拉菜单

javascript - 单击元素时 bootstrap 崩溃中断

javascript - 动态 react 选择菜单组件

html - 使用 z-index 将元素定位在 div 下

html - Wii 互联网 channel (Opera) 的 SVG

javascript - 将 HTML 复选框链接到 Javascript