遍历<a> tag in HTML

时间:2018-03-03 13:28:44

标签: jquery html

Going bonkers here! Please can someone tell me why I can't seem to apply a class 'greyout' to the a tag. I'm in the checkbox (this) and variable 'mydropID' reads "drop_billet" perfectly. But then my code doesn't seem to traverse properly to the a tag and I get error 'Cannot read property 'addClass' of undefined'. I know there's nothing wrong with the CSS. Many thanks for any help.

<label><input type="checkbox"  id="tr_billet" />Billet</label>

<select id="drop_billet" multiple="multiple">
  <option value="Carbon">Carbon</option>
  <option value="Stainless">Stainless</option>
</select>

<a href="#" id="TradeProdsID" class="RHLabelClass" >Details...</a>

.

var mydropID = '#' + ($(this).parent().next().attr('id')); 
$(mydropID).next().attr('id').addClass("greyout");

1 个答案:

答案 0 :(得分:0)

$(mydropID).next().attr('id')将返回a的ID而不是a#TradeProdsID本身的ID,因此如果您想要访问该元素,请使用$(mydropID).next()

请参阅下面的工作代码。

&#13;
&#13;
var mydropID = '#drop_billet'; 
$(mydropID).next().addClass("greyout");
&#13;
.greyout{
  background-color: #cccccc;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label><input type="checkbox"  id="tr_billet" />Billet</label>

<select id="drop_billet" multiple="multiple">
  <option value="Carbon">Carbon</option>
  <option value="Stainless">Stainless</option>
</select>

<a href="#" id="TradeProdsID" class="RHLabelClass" >Details...</a>
&#13;
&#13;
&#13;