[英]jQuery: check data-filter and replace spaces
我正在嘗試讓jQuery檢查此列表內的所有數據過濾器,以防萬一它在單詞之間找到一個空格,應將其替換為“,”。 (我需要那些是類)。
這是我嘗試過的方法,但是當我檢查控制台時,它說“替換不是功能”。
HTML
<ul class="filter-list" data-option-key="filter">
<li>
<a data-filter=".class one" class="" href="#filter">class one</a>
</li>
<li>
<a class="" href="#filter" data-filter=".class two">class two</a>
</li>
<li>
<a class="" href="#filter" data-filter=".class three">class three</a>
</li>
</ul>
jQuery的
$('.filter-list a[data-filter]').each(function () {
$(this).replace(/\s/g,", .");
});
基本上,當jQuery找到“ .class three”時,應將其替換為“ .class .three”。
在您的代碼中:
$('.filter-list a[data-filter]').each(function () {
$(this).replace(/\s/g,", .");
});
$(this)
是一個DOM元素。 您可以通過$(this).text()
獲得文本。 因此您的代碼可以是這樣的:
$('.filter-list a[data-filter]').each(function () {
$(this).text($(this).text().replace(/\s/g,", ."));
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.