如何在包含选择器中使用井号 (£)?



我试图隐藏所有包含 £0.00 但 £ 符号未正确传递的选项。有没有办法在选择器中使用它?

我已经尝试了几种传递井号的方法,但似乎都没有工作(设置为变量,使用 unicode 等(

这是我在源代码中的内容:

$('option:contains(£0.00)').hide();

这是我进入"查看源代码"时显示的内容:

$('select option:contains(£0.00)').remove();

您需要在包含和删除中使用引号,仅当字符串完全匹配时,它才有效:

$('select option:contains("£0.00")').remove();

事实证明,这是编码的问题。记事本++设置为UTF-8,但是当我从记事本++中获取代码时,将其粘贴到实际的Windows记事本.txt文件中,然后从那里保存它 - 它编码正确,£符号前面的奇怪字符消失了。

感谢@phuzi的指点!

您可以循环使用选项并检查文本是否为 = £0.00

$('option').each(function(){
if($(this).text() == '£0.00'){
$(this).hide();
}
})

这应该有效。

签入 HTML 如果您的井号是由 html实体或其他实体打印的,则可能会使您的选择器 https://www.toptal.com/designers/htmlarrows/currency/pound-sign/失败

如果"£0.00"不是选项节点中包含的唯一文本,则可以使用 indexOf 检查字符串是否包含另一个

所以上面的代码变成了这个

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
<option>£0.00</option>
<option>Some text £0.00</option>
<option>£23.00</option>
<option>£1.00 some other text</option>
<option>£0.00 some other text</option>
</select>
<script>
jQuery('option').each(function(){
// if ($(this).text().includes('£0.00')){.....
// or
console.log($(this).text())
console.log($(this).text().indexOf('£0.00'))
if($(this).text().indexOf('£0.00') > -1){
//$(this).hide()
$(this).css({background: 'red'});
}
})
</script>

最新更新