简体   繁体   English

我如何遍历li的url读取部分,然后使用Jquery将其作为类添加到li

[英]How do I loop through li's read part of a url then add that as a class to the li with Jquery

Hi I need some help with this. 嗨,我需要一些帮助。

Heres my code:- 这是我的代码:-

  <ul class="product nobullet clearfix nomargin">
      <li class="product1">
          <div class="img-holder">
          <a href="/Mens/Shower/Bodywash/p/704357">
          <img src="/medias/sys_master/front/src/8838871056414.jpg"/>
          </a>
          </div>
      </li>
      <li class="product2">
          <div class="img-holder">
          <a href="/Mens/Shower/shampoo/p/704357">
          <img src="/medias/sys_master/front/src/8838871056414.jpg"/>
          </a>
          </div>
      </li>
      <li class="product3">
          <div class="img-holder">
          <a href="/Mens/toiletries/deoderant/p/704357">
          <img src="/medias/sys_master/front/src/8838871056414.jpg"/>
          </a>
          </div>
      </li>
  <ul>

  <script>
  $('ul.product').children().has("li").each(function(){
      var url = $(this).find('div.img-holder a').attr('href');
      url = url.split("/");
      alert(url);
      $(this).children("li").addClass(url[3]);
  });
  </script>

It doesn't work I'm still new to jquery and could do with some help. 它不起作用,我对jquery还是很陌生,可以提供一些帮助。 I need to read each product class li's read the url take the 3 item using / as asperator then add this to the li class. 我需要阅读li的每个产品类,并使用/作为asperator读取URL取3项,然后将其添加到li类中。 The code needs to loop through all the product li's 代码需要遍历li的所有产品

There can be upto 20 product classes all start with product then have a number at the end eg product1 最多可以有20个产品类别,所有产品类别均以产品开头,然后以数字结尾,例如product1

Not sure if i've explained theat well enough so.... After the jquery code runs the html should look like this:- 不知道我是否已经足够好地解释了剧院。...。在jquery代码运行之后,html应该如下所示:-

  <ul class="product nobullet clearfix nomargin">
      <li class="product1 bodywash/">
          <div class="img-holder">
          <a href="/Mens/Shower/bodywash/p/704357">
          <img src="/medias/sys_master/front/src/8838871056414.jpg"/>
          </a>
          </div>
      </li>
      <li class="product2 shampoo">
          <div class="img-holder">
          <a href="/Mens/Shower/shampoo/p/704357">
          <img src="/medias/sys_master/front/src/8838871056414.jpg"/>
          </a>
          </div>
      </li>
      <li class="product3 deoderant">
          <div class="img-holder">
          <a href="/Mens/toiletries/deoderant/p/704357">
          <img src="/medias/sys_master/front/src/8838871056414.jpg"/>
          </a>
          </div>
      </li>
  <ul>

Check this 检查一下

As you are iterating over li's you just need to use $(this).addClass(url[3]); 当您遍历li时,只需要使用$(this).addClass(url[3]);

 $('ul.product li').each(function(){
      var url = $(this).find('div.img-holder a').attr('href');
      url = url.split("/");
      alert(url);
      $(this).addClass(url[3]);
  });

Nitin's answer is almost correct. 尼丁的答案几乎是正确的。

Firstly since you're new to jQuery it's worth explaining that your selector isn't matching <li> s like you think it is 首先,由于您是jQuery新手,所以值得解释一下,选择器与<li>不匹配,就像您认为的那样

$('ul.product').children().has("li")

Will match any children of the ul which have a descendant which is an <li> . 将匹配ul的后代为<li>的所有子代。 Since the list items are the children of the list they themselves will not have list items as children. 由于列表项是列表的子项,因此它们本身不会将列表项作为子项。

 $('ul.product li').each(function(){
      var url = $(this).find('div.img-holder a').attr('href');
      url = url.split("/");
      alert(url);
      $(this).addClass(url[3]);
  });

With reference to this code ^ it's worth noting that JavaScript is a zero-based language. 参考此代码^,值得注意的是JavaScript是一种基于零的语言。 So you'll want to use url[2] to get the third item in that array 因此,您需要使用url[2]获取该数组中的第三项

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

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