开发者

selection on condition

开发者 https://www.devze.com 2022-12-21 12:34 出处:网络
I\'m trying and trying and can\'t make it working. I have a ul class=\"first\" . From all the ul\'s \"first\" in the template I need to select only those containing ul class=\"second\" 开发者_高级运维

I'm trying and trying and can't make it working. I have a ul class="first" . From all the ul's "first" in the template I need to select only those containing ul class="second" 开发者_高级运维and for the selected change h2 href to "#". Problem is that when I find this specific "first" with "second" I can change "second" parameters with no problems but in no way I can figure how to influence "first".

Here's the link with both js and html at once : http://jsbin.com/acoqi/edit

HTML :

<ul id="Menu">
<li>
  <span id="primary">
      <h1>
          <a href="#">Bags</a>
      </h1>
  </span>
  <ul class="first">
      <li>
          <span id="secondary">
              <h2>
                  <a href="/category/category/bags_/">Bags</a>
              </h2>
          </span>
          <ul class="second">
              <li>
                  <h3>
                      <a href="/category/category/shoulderbag/">Shoulder Bags</a>
                  </h3>
              </li>
          </ul>
      </li>
      <li>
        ...
      </li>
  </ul>
</li>
<li>
  <ul class="first">
     <li>        
          <span id="secondary">
              <h2>
                  <a href="/category/category/bags_/">Bags</a>
              </h2>
          </span>          
    </li>
  </ul>

JS:

    var second = $(".second");
    if(second.length && second.closest(".first")) {
       $(".first h2 a").attr("href", "#");
    };


I think you're looking for the :has selector

$('ul.first:has(ul.second) h2 a').attr("href", "#");

Or, closer to what you're doing:

var second = $(".second");
if(second.length && second.closest(".first")) {
   second.closest(".first").find("h2 a").attr("href", "#");
};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号