简体   繁体   English

获取父元素javascript

[英]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.我试图获得的类是“temp_pid”,现在我有了上面的代码,当时我的脚本得到了所有的 temp_pid 类,总共有 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。它将选择父元素中的元素。我希望它会起作用

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

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