简体   繁体   中英

Getting parent element javascript

I have a bit of a problem which i just can't get my head round, i could but it would just be defeating the whole purpose of getting better. So with that being said i have set of elements which all have the same class name and when i select a button i want it to only get me the elements within that parent that is clicked.

<article class="col-xs-12 col-sm-3">
<a href="http://79.170.43.200/shaveandsubscribe2.co.uk/shaveandsubscribe2/index.php/gillette-fusion-proshield.html?___SID=U">
<h3>Gillette Fusion ProShield</h3> 
 <pid class="temp_pid" style="display:none;">366730415</pid>
 <pid2 class="temp_pid" style="display:none;">373545146</pid2>
 <pid3 class="temp_pid" style="display:none;">373546370</pid3>
 <img src="http://79.170.43.200/shaveandsubscribe2.co.uk/shaveandsubscribe2/media/catalog/product/cache/1/image/265x/9df78eab33525d08d6e5fb8d27136e95/g/i/gillette-fusion-proshield.jpg" alt="Gillette Fusion ProShield" class="img-responsive">
 </a>
    <div class="1" style="display:none;">

  <button type="button" id="1" title="Proceed" class="button btn-cart" onclick="getUrl(this.id);"><span><span>Proceed</span></span></button>

   <input type="hidden" id="x1" class="rrp_price" name="rrp_price" value="$19.4900">

   <input type="hidden" id="y1" class="spcl_price" name="spcl_price" value="$19.4900">

    </div>

  <button class="price-change" id="1" value="£19.49" name="£19.49" onclick="getSet(this.id);">Select</button>



  </article>

the class i am trying to get is "temp_pid" now i have the code above there is effectively 3 lots of that at the minute my script get's all of the temp_pid classes which in total is 9.

    jQuery('body').on('click','.price-change',function(){
localStorage.removeItem('priceChange');
localStorage.setItem('priceChange', "priceChange");
// alert(localStorage.getItem("priceChange")); 

// cratejoy script to capture product ids aka <pid> etc...
if(jQuery('.price-change').hasClass('selected')){
 var x = document.getElementsByClassName('temp_pid');
 for(var i = 0; i < x.length; i ++){
   console.log(x[i].innerHTML);
 }

};

});

尝试 data-id。它将选择父元素中的元素。我希望它会起作用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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