简体   繁体   English

使用JavaScript删除主题标签

[英]Remove hashtags with Javascript

Is there a way to hide all hashtags on the page with javascript? 有没有办法用javascript隐藏页面上的所有主题标签?

For instance, I have a tag cloud underneath each post on a website I'm making. 例如,我正在制作的网站上的每个帖子下方都有一个标签云。 Each tag looks something like: #myhashtag 每个标签看起来像: #myhashtag

I want javascript (possibly css?) to run through the document and hide the "#" so that the tag ends up simply looking like: myhashtag 我希望javascript(可能是CSS?)在文档中运行并隐藏“#”,以便标记最终看起来像: myhashtag

is this possible? 这可能吗?

Let the hash tags be elements with a specific class. 让hash标签成为具有特定类的元素。 (Just edit the CSS selector accordingly if you have other identification marks.) Than 8 lines jQuery should do the trick: (如果您有其他识别标记,则只需相应地编辑CSS选择器。)jQuery应该做到8行以上:

  $('.my-hash-tag').each(function(i, elem) {
      var $elem = $(elem), text = $elem.text();

      if(text.length > 0 && text.charAt(0) == '#') {
         $elem.text(text.substring(1));
      }
  });
var doc = document.body;
doc.innerHTML = doc.innerHTML.replace(/(\B)#(\w+)\b/g, '$2');

This will do the job, comments explain how it works. 这将完成工作,注释说明其工作方式。 Currently it assumes the tags are inside anchors and inside a div with id #cloud however this is easily edited, just use a different element selector the concept remains the same. 目前,它假定标签位于锚和ID为#cloud的div内,但这很容易编辑,只是使用不同的元素选择器,其概念仍然相同。

var tagCloud = document.getElementById("cloud"); // Get tag cloud element
var tags = tagCloud.getElementsByTagName('a'); // Find all anchors within cloud (If they aren't anchors change this to containing elements or replace with a class search .etc
for (var i=0, max=tags.length; i < max; i++) { // Loop through tags
     tags[i].innerHTML = tags[i].innerHTML.replace("#", ""); // Remove #'s
}

Working fiddle: http://jsfiddle.net/3ayhA/1/ 工作提琴: http : //jsfiddle.net/3ayhA/1/

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

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