2015-09-14 163 views
0

我在wordpress中有一个选项列表,但是它在子级菜单的自动a中生成 - 在它之前。现在我想循环它们,但不能得到它似乎工作。删除选择选项中的所有“ - ”

我锁定了我的菜单项并尝试循环删除“ - ”。

var $menuitem = $(".hasCustomSelect .menu-item"); 

$menuitem.filter(function(){ 
    return $.trim($(this).text()) == '-' 
}).remove(); 

小codepen

http://codepen.io/denniswegereef/pen/NGGrgR

+1

这是因为装饰不仅能消除空白。你不能修剪删除其他字符。您需要使用不同的工具,例如'regex'。 –

+0

从来没有听说过,要去看看。 – Dennis

+0

只有一个'-'的条目会发生什么?像9.5到10之间?这是你在http://jsfiddle.net/j08691/acfsnmn1/之后? – j08691

回答

0

我可以看到 - Vs的 - 从codepen或从下面的代码尝试复制粘贴

而且我猜你想使用

var $menuitem = $(".hasCustomSelect .menu-item"); 

$menuitem.filter(function(){ 
    return $.trim($(this).text()).indexOf('—')>-1;//all values containing `—` 
}).remove(); 

或者如果你真的想删除单一的 - 比

return $.trim($(this).text())=='—'; //the value that is `—` 

更新的代码笔

+0

如果我结合代码也删除短破折号不起作用。这只会消除两者之间的空隙。 – Dennis

0

如果删除分隔符选项,是您想要的,那么imtheman有一个solution for you

如果你想从菜单文字说明(我对你所需要的第一个猜测)删除开头的破折号,那么你可以用下面的代码片段去:

var $menuitem = $(".hasCustomSelect .menu-item"); 
$.each($menuitem, function(key, val){ 
    $(val).text($(val).text().replace(/—+ /,'')); 
});