如何使用jQuery查找和替换HTML实体?
我使用HTML表为Drupal站点构建了一个日历模板,并且我有jQuery为每个空单元格添加一个类“无文本”:
$('table.calendar td:empty').addClass('no-text');
这很好用,但我的问题是CMS WYSIWYG编辑器自动添加HTML实体
清空细胞。 因此,我试图事先找到并替换具有“真实”空间的实体,但是jQuery无法找到它们:
$('table.calendar td').each(function() { var $this = $(this); var t = $this.text(); $this.text(t.replace('[entity here]','')); });
替换普通字符串时,此代码段工作正常,但
似乎是不同的东西!
所以我的问题是:jQuery如何用于搜索和替换HTML实体?
最简单的事情就是
$this.text(t.replace('\u00a0',''));
其中\u00a0
是
的unicode字符
尝试
replace(/& nbsp;/g, '');
没有&符号后的空格。
你试过.html()吗?
$ this.html( ”);
如果您的nbsp位于标记内,而不是外部js文件,则需要对html进行两次编码:
这是另一种有效的选择。
var nbsp = unescape("%a0"); // a0 is hex code point for $this.text(t.replace(nbsp,''));