繁体   English   中英

(Javascript / jQuery)使用带有特定文本的ap定位div

[英](Javascript / jQuery) Target a div with a p with a specific text

我现在正努力奋斗几个小时,如何定位包含ap并带有指定文本的div

的HTML

<div class="NavBar_Row_Button2"><p>DONATE</p></div>
<div class="NavBar_Row_Button2"><p>CONTACT</p></div>
<img src="Design/Erk%20logo.png" alt="Erk Logo" id="NavBar_Logo">
<img src="Design/ycoGM9dcE.png" id="Drop_Menu">

jQuery'

$(document).ready(function(){
  $('.NavBar_Row_Button2 P').hover(function(){
    $(this).css('color', '#00ddcb'); 
    $(this.parentElement).css('background-color', '#00ddcb');    
  });

  $('.NavBar_Row_Button2 P').mouseleave(function(){
      $(this).css('color', 'white'); 
      $(this.parentElement).css('background-color', 'white');
  });

  $('.NavBar_Row_Button1 P').hover(function(){
      $(this).css('color', '#00ddcb'); 
      $(this.parentElement).css('background-color', '#00ddcb');
  });
  $('.NavBar_Row_Button1 P').mouseleave(function(){
      $(this).css('color', 'white'); 
      $(this.parentElement).css('background-color', 'white');
  });
  if ($(this).is('div:contains("HEY")')) {
      alert('right')  
  };
});

因此,您也可以看到我更改了一些背景颜色,但是如果带ap且带有特殊文本的spfic div为:hover,我也想对图像进行某些操作:悬停如何定位此spefic对象,在这种情况下,这是具有div的div

尝试过诸如包含和其他东西之类的东西,但对我不起作用

$('.NavBar_Row_Button2 P').hover(function(){
    if($(this).text() == 'Text you are looking for')
    {
      $(this).css('color', '#00ddcb'); 
      $(this.parentElement).css('background-color', '#00ddcb');
    }
});

您可以使用:contains()选择器:

$('.NavBar_Row_Button2 p:contains("DONATE")').hover(function(){
    $(this).css({'color': 'red'});
});

但是,我可能建议您尝试使用其他识别方法,以将文本内容与行为分开,例如添加类或其他属性以识别目标。

由于看起来好像不是为这些<p>元素动态生成文本,所以为什么不只给它们一个唯一的ID并选择这种方式呢? 就像是:

HTML:

<div class="NavBar_Row_Button2">
   <p id="donate">DONATE</p>
 </div>

JavaScript:

$('#donate').hover(function() {
 //Whatever you want to do here
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM