Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何确定一个元素是否只包含一个£;用JQuery签名?_Javascript_Jquery_Contains - Fatal编程技术网

Javascript 如何确定一个元素是否只包含一个£;用JQuery签名?

Javascript 如何确定一个元素是否只包含一个£;用JQuery签名?,javascript,jquery,contains,Javascript,Jquery,Contains,假设我有两个div: <div>&pound; 21.99</div> <div>&pound;</div> 试试,过滤器 我发现了另一个关于SO的问题以及相关信息: 我认为问题在于浏览器正在转换HTML转义字符,因此您需要查找实际字符。。。我测试了这个,它是有效的 $(document).ready(function(){ $('div').each(function(){ if ($(this).html() == "£

假设我有两个div:

<div>&pound; 21.99</div>
<div>&pound;</div>
试试,过滤器

我发现了另一个关于SO的问题以及相关信息:


我认为问题在于浏览器正在转换HTML转义字符,因此您需要查找实际字符。。。我测试了这个,它是有效的

$(document).ready(function(){
 $('div').each(function(){
  if ($(this).html() == "£"){
   $(this).addClass("pound");
  }
 })
})

tvanfosson的代码允许在符号的任意一侧使用空格,但正则表达式需要稍加修改:

$('div:contains("&pound;")').each( function() {
    var text = $(this).html();
    if (text && text.match(/^\s*&pound;\s*$/)) {
        $(this).addClass('pound');
        }
    });

请注意表示字符串开头和结尾的“^”和“$”。

您可能需要调整正则表达式。您可能需要搜索与英镑符号关联的unicode值。尝试通过firefox/firebug调试器单步执行代码,并查看每个div的innerHTML值。如果contains()似乎没有选择所需的div,则删除它,以便更好地了解javascript在div中看到的内容<代码>&磅是一个HTML转义序列,而不是Javascript序列。。看起来Javascript会看到实际的英镑符号是合理的,尽管我想这两种方式都是合理的。。
$(document).ready(function(){
 $('div').each(function(){
  if ($(this).html() == "£"){
   $(this).addClass("pound");
  }
 })
})
$('div:contains("&pound;")').each( function() {
    var text = $(this).html();
    if (text && text.match(/^\s*&pound;\s*$/)) {
        $(this).addClass('pound');
        }
    });