[英]How to get data value of a div that has no ID?
Below is part of some code on an html page that lists shopping cart products. 以下是html页面上列出购物车产品的部分代码的一部分。 Using JavaScript/jQuery, I need to be able to loop through the li
items and get the div "data-" values for each. 使用JavaScript / jQuery,我需要能够遍历li
项并获得每个div的“data-”值。 The issue that I am having is that there are no IDs for the div that has the data- value (). 我遇到的问题是没有具有data-value()的div的ID。 I only see the div for "CategoryContent". 我只看到“CategoryContent”的div。
<div class="Block CategoryContent Moveable Panel" id="CategoryContent">
<ul class="ProductList ">
<li class="Odd">
<div class="ProductImage QuickView" data-product="577">
<a href="http://example.com/sweater-vest-v-neck-cotton/"><img src="http://cdn3.example.com/products/577/images/1731/2311-.jpg?c=2" alt="Sweater Vest V-Neck Cotton" /></a>
</div>
<div class="ProductDetails">
<a href="http://example.com/sweater-vest-v-neck-cotton/" class=" pname">Sweater Vest V-Neck Cotton</a>
</div>
<em class="p-price">$45.04</em>
<div class="ProductPriceRating">
<span class="Rating Rating0">
<img src="http://cdn3.example.com/templates/custom/images/IcoRating0.png?t=" alt="" style="" />
</span>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="http://example.com/sweater-vest-v-neck-cotton/" class="btn icon-Choose Options" title="Choose Options">Choose Options</a>
</div>
</li>
</ul>
</form>
</div>
So, there is only one li
item here, on a typical page, there are up to 9. My goal is to use JavaScript to get the data-product
values and then use that to look up a better image thumbnail and have it replaced. 所以,这里只有一个li
项目,在典型页面上,最多有9个。我的目标是使用JavaScript获取data-product
值,然后使用它来查找更好的图像缩略图并将其替换。 So, how do I get the value set for data-product
? 那么,我如何获得data-product
的价值集?
Quite easy: 很简单:
// Loop through all list items of ul.ProductList:
$("ul.ProductList li").each(function (index, element) {
// Find the element with attribute data-product:
$dp = $(element).find("[data-product]");
// Get the value of attribute data-product:
var product = $dp.attr("data-product");
// Now set the high quality thumbnail url:
var url = "/images/hq/" + product + ".png"; // Note that this is just an example
// Here you can use $(element) to access to current li (and the img):
$(element).find('.ProductImage img').attr('src', url);
});
You can use this: 你可以用这个:
$("#CategoryContent div[data-product]").each(function(){
alert($(this).attr('data-product'));
});
Pure JS: 纯JS:
var divs = document.querySelectorAll('#CategoryContent div[data-product]');
var index = 0, length = divs.length, prodIds = [];
for ( ; index < length; index++) {
prodIds.push(divs[index].getAttribute('data-product'));
}
Fiddle: http://jsfiddle.net/we5q7omg/ 小提琴: http : //jsfiddle.net/we5q7omg/
You could use the class name to get the data-product 您可以使用类名来获取数据产品
var products = document.getElementsByClassName("ProductImage");
for(var i=0; i<products.length;i++)
{
console.log(products[i].getAttribute("data-product"));
}
Using JQuery, you can get the elements with the data-product
attribute by simply calling 使用JQuery,您只需调用即可获取具有data-product
属性的元素
$('[data-product]')
// Or if you only want the data-product elements within the UL.
$('ul').find('[data-product]')
From there you can simply do pull the products from the elements. 从那里你可以简单地从元素中拉出产品。 For Example: 例如:
var products = $('[data-product]').map(function() {
return $(this).data('product');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.