Jquery查找具有特定文本的类,如果条件为true,则更改父类的css

Jquery查找具有特定文本的类,如果条件为true,则更改父类的css,jquery,Jquery,我正在尝试使用Jquery实现以下功能,但我不确定如何实现。任何帮助都将不胜感激。多谢各位 <script> $a = array(1,2); if ($('.box:contains($a)')){ $('.inbox' //the .inbox where .box text = $a).css(//something); } </script> <body> <div class='inbox'> <div cla

我正在尝试使用Jquery实现以下功能,但我不确定如何实现。任何帮助都将不胜感激。多谢各位

<script>
$a = array(1,2);

if ($('.box:contains($a)')){
    $('.inbox' //the .inbox where .box text = $a).css(//something);
}

</script>

<body>
<div class='inbox'>
    <div class='box'>1</div>
</div>

<div class='inbox'>
    <div class='box'>2</div>
</div>

<div class='inbox'>
    <div class='box'>3</div>
</div>

$a=数组(1,2);
如果($('.box:包含($a)')){
$('.inbox'//.inbox where.box text=$a.css(//something);
}
1.
2.
3.

jQuery有一个名为.parent()的方法,该方法返回父元素

e、 g:


$(文档).ready(函数(){
$('span:contains(“foo”)).parent().css('color','red');
});
福
酒吧
像这样的东西

$(document).ready(function () {
  $a = [1, 2];
  $('.box').each(function () {
    if ($.inArray(parseInt(this.innerText), $a) > -1) {
      $(this).closest('.inbox').css({
        border: "5px solid red"
      });
    }
  });
});

检查工作小提琴

$a=数组(1,2);不是很好的synthax,应该是$a=新数组(1,2);或$a=[1,2]@烘焙-谢谢,错过了OP想要将css应用于父元素的要点,编辑了我的代码。大家好,谢谢你们的回复!!我已经试过了代码,但它对我不起作用。我期望的结果是,数字为1和2的.inbox的边框将变为红色。假设我的css('border','5px实心红色')。我遗漏了什么吗?@Lynnie-检查我的最新答案,包括为你准备的小提琴。嗨,Ghabriel,谢谢你的回复。但是“:contains”由于某种原因似乎无法接受变量。
$(document).ready(function () {
  $a = [1, 2];
  $('.box').each(function () {
    if ($.inArray(parseInt(this.innerText), $a) > -1) {
      $(this).closest('.inbox').css({
        border: "5px solid red"
      });
    }
  });
});