Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Jquery 如果此文本包含在H1中,请仅对该H1执行某些操作,而不是对其他H1执行某些操作_Jquery_Select_Filter_Element_Contains - Fatal编程技术网

Jquery 如果此文本包含在H1中,请仅对该H1执行某些操作,而不是对其他H1执行某些操作

Jquery 如果此文本包含在H1中,请仅对该H1执行某些操作,而不是对其他H1执行某些操作,jquery,select,filter,element,contains,Jquery,Select,Filter,Element,Contains,我很难弄清楚如何只选择包含文本的元素。现在,上面的代码将把ever h1的颜色变成#09f当元素悬停时,它应该只把匹配的h1的颜色变成#09f 我试图为不同的用例找出这个问题。 有人知道只对匹配元素执行操作的最佳方法吗? 是否为.filter():包含()。包含()。is()?我想您需要这样的东西,它只会在被悬停的h1中查找文本“三”,并只更改h1的颜色: $('h1').hover(function(){ if ($('h1:contains("three")')) { $(thi

我很难弄清楚如何只选择包含文本的元素。现在,上面的代码将把ever h1的颜色变成
#09f
当元素悬停时,它应该只把匹配的h1的颜色变成
#09f

我试图为不同的用例找出这个问题。 有人知道只对匹配元素执行操作的最佳方法吗?
是否为.filter():包含()。包含()。is()?

我想您需要这样的东西,它只会在被悬停的h1中查找文本“三”,并只更改h1的颜色:

$('h1').hover(function(){
  if ($('h1:contains("three")')) {
    $(this).css({'color':'#09f'})
  }

    //$('h1').text().filter('three').css( 'color', '#09f' );
})

我想你想要这样的东西,只会在被悬停的h1中查找文本“三”,并且只改变h1的颜色:

$('h1').hover(function(){
  if ($('h1:contains("three")')) {
    $(this).css({'color':'#09f'})
  }

    //$('h1').text().filter('three').css( 'color', '#09f' );
})
直接使用
.is()

$('h1').hover(function(){
  if ($(':contains("three")', this).length) {
    $(this).css({'color':'#09f'})
  }
})
直接使用
.is()

$('h1').hover(function(){
  if ($(':contains("three")', this).length) {
    $(this).css({'color':'#09f'})
  }
})

您只需在
h1
选择器上使用
:contains

$('h1').hover(function() {
    if($(this).is(':contains("three")')) {
        $(this).css('color', '#09f')
    }

    // $('h1').text().filter('three').css( 'color', '#09f' );
})
工作


注意:由于没有为
hover()
提供out处理程序,css颜色保持不变,但您可以使用。这里有一个解决方法。

您可以在
h1
选择器上使用
:contains

$('h1').hover(function() {
    if($(this).is(':contains("three")')) {
        $(this).css('color', '#09f')
    }

    // $('h1').text().filter('three').css( 'color', '#09f' );
})
工作


注意:由于没有为
hover()
提供out处理程序,css颜色保持不变,但您可以使用。这里有一个例子。

我对
.contains()
规范的理解是它只接受DOM元素,而不接受文本。你知道另一个
.contains()
方法吗?@jfriend00:对不起,现在改为
.is()
了。我对
.contains()
规范的理解是它只接受DOM元素,不接受文本。您知道另一个
.contains()
方法吗?@jfriend00:对不起,现在改为
.is()