jQuery按ID查找元素并获取p.class内容

时间:2017-08-19 04:51:56

标签: javascript jquery

任何人都能看到我在这里做错了什么?我找到了ID,知道他们正在选择,但它没有返回段落内容的价值?

$.each(value, function(i, item) {           
    var getitem_price = (item.price);
    var monetizeitemprice = Shopify.formatMoney(getitem_price, '{{ shop.money_format }}');
    //finds image, creates url, cartfinalimage used instead of item.image later in the table
    var imageurl = (item.image);
    var varsize = ('small');
    var matches = imageurl.match(/(.*\/[\w\-\_\.]+)\.(\w{2,4})/);
    var cartfinalimage =  (matches[1] + '_' + varsize + '.' + matches[2]);
    var line_item = i+1;
    //console.log(item);
    console.log({% include 'ly-title' with item.variant %}{{ ly_translation }});
    var product_id = "#cart-info #product-" + item.id;
    console.log("collected id:" + product_id);
    var new_product_id = $(product_id).find('.cart_title').closest('p.item-variant').text();
    console.log("collected variant text:" + new_product_id);
    if(item.product_type != "Tech Pillow"){
      var verified_variant_title = new_product_id;
    } else {
      var verified_variant_title = "";
    }
    $("<tr id='product-" + item.id + "' class='"+item.handle + " item'><td id='invisible' class='cart_amount'> <input class='text' type='text' size='4' name='updates"+line_item+"' id='updates_"+line_item+"' value='"+ item.quantity +"' onfocus='this.select();' class='replace' /> </td><td class='cart_image'><a href='"+ item.url +"'><img class='cartimage' src='"+ cartfinalimage +"'  alt='"+ item.title +"' /></a></td><td class='cart_title'> <a href='"+item.url +"'>"+ item.product_title +"</a><p class='item-variant'>"+ verified_variant_title +"</p></td><td class='cart_itemprice'><span class='quantity'>"+ item.quantity +"</span> x "+ monetizeitemprice +"</td><td class ='cart_remove'><a onClick='removecartitem("+ line_item +"); return false;'>remove</a></td></tr>").appendTo(uc);
});

控制台正在返回:

  

收集的ID:#cart-info#product-48985959123

     

收集的变体文字:

     

收集的ID:#cart-info#product-48985341011

     

收集的变体文字:

     

收集的ID:#cart-info#product-48985611923

     

收集的变体文字:

HTML:

<table>
          <colgroup>
            <col class="checkout-image">
            <col class="checkout-info">
            <col class="checkout-price">
            <col class="checkout-quantity">
            <col class="checkout-totals">
            <col class="checkout-delete">
          </colgroup>

          <tbody id="updating_cart">
          <!-- this is what we re-create with the script -->

            <tr id="product-48985810387" class="tech-pillow-black-sapphire item">

              <td id="invisible" class="cart_amount cartvis"> <!-- FOR THE FUTURE -->
                <input class="text" type="text" size="4" name="updates[1]" id="updates_1" value="27" onfocus="this.select();"> 
              </td>

              <td class="cart_image">
                <a href="/products/tech-pillow-black-sapphire">
                  <img class="cartimage" src="//cdn.shopify.com/s/files/1/1425/5898/products/techpillow-black-sapphire-hero_5f9683e7-9ad1-499f-9848-203109d1f42d_small.jpg?v=1502494770" alt="Tech Pillow - Black Sapphire">
                </a>
              </td>

              <td class="cart_title"> 
                <a href="/products/tech-pillow-black-sapphire">Tech Pillow - Black Sapphire</a> 

              </td>

              <td class="cart_itemprice">
                <span class="quantity">27</span> x 29,00 €
              </td>

              <!--td class ="cart_remove"><a onClick="removecartitem(1); return false;">




entfernen</a></td-->

            </tr>
          <!-- this is what we re-create with the script -->

            <tr id="product-48985346899" class="acoustic-sofa-eco-weave item">

              <td id="invisible" class="cart_amount cartvis"> <!-- FOR THE FUTURE -->
                <input class="text" type="text" size="4" name="updates[2]" id="updates_2" value="2" onfocus="this.select();"> 
              </td>

              <td class="cart_image">
                <a href="/products/acoustic-sofa-eco-weave">
                  <img class="cartimage" src="//cdn.shopify.com/s/files/1/1425/5898/products/acoustic-eco-weave_grande_8d47cd0e-1889-4303-9db8-52fb72af5abf_small.jpg?v=1502494366" alt="Acoustic Sofa - Eco Weave">
                </a>
              </td>

              <td class="cart_title"> 
                <a href="/products/acoustic-sofa-eco-weave">Acoustic Sofa - Eco Weave</a> 



                            <p class="item-variant">Bezug + Füllung</p>



              </td>

              <td class="cart_itemprice">
                <span class="quantity">2</span> x 249,00 €
              </td>

              <!--td class ="cart_remove"><a onClick="removecartitem(2); return false;">




entfernen</a></td-->

            </tr>
          <!-- this is what we re-create with the script -->

            <tr id="product-48985407827" class="contempo-package-blue-jazz item">

              <td id="invisible" class="cart_amount cartvis"> <!-- FOR THE FUTURE -->
                <input class="text" type="text" size="4" name="updates[3]" id="updates_3" value="2" onfocus="this.select();"> 
              </td>

              <td class="cart_image">
                <a href="/products/contempo-package-blue-jazz">
                  <img class="cartimage" src="//cdn.shopify.com/s/files/1/1425/5898/products/bj_1ffdface-d0f5-4bc9-a26f-b8d718d9f9bc_small.png?v=1502494437" alt="Contempo Package (Blue Jazz)">
                </a>
              </td>

              <td class="cart_title"> 
                <a href="/products/contempo-package-blue-jazz">Contempo Package (Blue Jazz)</a> 



                            <p class="item-variant">Cover+Bean Filling</p>



              </td>

              <td class="cart_itemprice">
                <span class="quantity">2</span> x 627,30 €
              </td>

              <!--td class ="cart_remove"><a onClick="removecartitem(3); return false;">




entfernen</a></td-->

            </tr>
          <!-- this is what we re-create with the script -->

            <tr id="product-48985959123" class="wing-ottoman-mudhoney-dune-sunbrella item">

              <td id="invisible" class="cart_amount cartvis"> <!-- FOR THE FUTURE -->
                <input class="text" type="text" size="4" name="updates[4]" id="updates_4" value="1" onfocus="this.select();"> 
              </td>

              <td class="cart_image">
                <a href="/products/wing-ottoman-mudhoney-dune-sunbrella">
                  <img class="cartimage" src="//cdn.shopify.com/s/files/1/1425/5898/products/jets-mudhoney-wing-hero_8940a6c4-45d5-4a10-908a-b5b0fc5387de_small.png?v=1502494877" alt="Wing Ottoman - Mudhoney Dune (Sunbrella®)">
                </a>
              </td>

              <td class="cart_title"> 
                <a href="/products/wing-ottoman-mudhoney-dune-sunbrella">Wing Ottoman - Mudhoney Dune (Sunbrella®)</a> 



                            <p class="item-variant">Bezug + Füllung</p>



              </td>

              <td class="cart_itemprice">
                <span class="quantity">1</span> x 219,00 €
              </td>

              <!--td class ="cart_remove"><a onClick="removecartitem(4); return false;">




entfernen</a></td-->

            </tr>
          <!-- this is what we re-create with the script -->

            <tr id="product-48985341011" class="acoustic-sofa-black-sapphire item">

              <td id="invisible" class="cart_amount cartvis"> <!-- FOR THE FUTURE -->
                <input class="text" type="text" size="4" name="updates[5]" id="updates_5" value="4" onfocus="this.select();"> 
              </td>

              <td class="cart_image">
                <a href="/products/acoustic-sofa-black-sapphire">
                  <img class="cartimage" src="//cdn.shopify.com/s/files/1/1425/5898/products/acoustic-black-sapphire_81df887d-4836-44de-96b5-7477cea9b182_small.jpg?v=1502494364" alt="Acoustic Sofa - Black Sapphire">
                </a>
              </td>

              <td class="cart_title"> 
                <a href="/products/acoustic-sofa-black-sapphire">Acoustic Sofa - Black Sapphire</a> 



                            <p class="item-variant">Bezug + Füllung</p>



              </td>

              <td class="cart_itemprice">
                <span class="quantity">4</span> x 249,00 €
              </td>

              <!--td class ="cart_remove"><a onClick="removecartitem(5); return false;">




entfernen</a></td-->

            </tr>
          <!-- this is what we re-create with the script -->

            <tr id="product-48985611923" class="mod-3-movie-couch-eco-weave item">

              <td id="invisible" class="cart_amount cartvis"> <!-- FOR THE FUTURE -->
                <input class="text" type="text" size="4" name="updates[6]" id="updates_6" value="1" onfocus="this.select();"> 
              </td>

              <td class="cart_image">
                <a href="/products/mod-3-movie-couch-eco-weave">
                  <img class="cartimage" src="//cdn.shopify.com/s/files/1/1425/5898/products/IMG_1476_ecoweave_mod3moviecouch_small.jpg?v=1502494576" alt="Mod 3 Movie Couch - Eco Weave">
                </a>
              </td>

              <td class="cart_title"> 
                <a href="/products/mod-3-movie-couch-eco-weave">Mod 3 Movie Couch - Eco Weave</a> 



                            <p class="item-variant">Bezug + Füllung</p>



              </td>

              <td class="cart_itemprice">
                <span class="quantity">1</span> x 499,00 €
              </td>

              <!--td class ="cart_remove"><a onClick="removecartitem(6); return false;">




entfernen</a></td-->

            </tr>

          </tbody>
        </table>

1 个答案:

答案 0 :(得分:0)

.closest()在祖先看起来向上以找到匹配的元素...

.find()在子项中看向下

尝试直接从p

获取tr
$(product_id).find('p.item-variant').text();