如果另一个类中的文本包含,则隐藏类。 。 。

时间:2013-08-07 17:16:24

标签: jquery css

我正在使用一个包含多个步骤的表单,如果另一个类,.RegCurrent包含文本“Step 1 Selection”,我想隐藏一个已经应用了类.newtest的文本范围。

<ol class="steps">
<li class="RegCurrent">Step 1 Selection</li>
<li class="notcurrent">Step 2 Selection</li>
<li class="notcurrent">Step 3 Selection</li>
</ol>

<div class="singleCol">
<span class="newtest">
<h1>some text</h1>
<p>some more text</p>
</span>
<p>some text and tables</p>
</div>

我尝试过:

<script>
$(document).ready(function() {
if( $('.RegCurrent:contains("Step 1 Selection")') {
$('.newtest').hide();
}
});
</script>

2 个答案:

答案 0 :(得分:2)

你必须在条件中使用带有选择器的length,否则你不会得到错误,因为当选择器没有返回任何对象时你将获得jQuery对象事件。你也错过了条件的右括号。

<强> Live Demo

$(document).ready(function() {
  if( $('.RegCurrent:contains("Step 1 Selection")').length) {
    $('.newtest').hide();
  }
});

答案 1 :(得分:0)

if($('.RegCurrent').text() == "Step 1 Selection"){
    $('.newtest').hide();
}