从DIV内部的元素获取值
Get values from elements inside DIV
我需要在单击一个按钮时获得div内部的值,该按钮位于该div内部
<div class="products__item">
<div class="products__content">
<a class="products__title" href="#">Altec Lansing Octiv Duo M202 акустическая система акустическая система</a>
<div class="products__priceholder">
<p class="products__price"><strong>86 590</strong> руб.</p>
<small class="products__id">ID. 10906</small>
</div>
<p class="exist">В наличии</p>
<div class="products__buttonholder">
<a href="#" data-reveal-id="buyModal" class="button button-buy-modal">Купить</a>
</div>
</div>
</div>
当我点击.button buy modal时,我需要从.products_title.products_price和.productt_id中获取值,但问题是我们有很多相同的div(产品卡),其中有很多按钮。我想我应该用$(这个)之类的东西,但实际上我不知道怎么用。
我正在尝试测试这样的东西,但它不起作用:
$("a.button-buy-modal").click(function () {
$(this).find().closest('.products__priceholder').addClass('test1');
})
这里有一个解决方案:
$("a.button-buy-modal").click(function () {
var prTitle = $(this).parent().parent().children('.products__title').html();
var prPrice = $(this).parent().parent().children('.products__priceholder').children('.products__price').children('strong').html();
var prId = $(this).parent().parent().children('.products__priceholder').children('.products__id').html();
var prImage = $(this).parent().parent().parent().children('.products__imageholder').children('.products__thumbnail').attr('src');
console.log(prTitle);
console.log(prPrice);
console.log(prId);
console.log(prImage);
})
$(this).parent().parent().find('.products__price').text()
会给你这个-"86 590 руб."
$(this).parent().parent().find('.products__price').html()
会给你这个——"<strong>86 590</strong> руб."
要了解更多关于它的信息,比如如何选择任何特定的DOM元素并读取或操作它等,请阅读此处-http://api.jquery.com/category/selectors/
您可以向父div添加一个id,或者使用eq(x)来选择正确的组。
<div id="mydiv1" class="products__item">
<div class="products__content">
<a class="products__title" href="#">Altec Lansing Octiv Duo M202 акустическая система акустическая система</a>
<div class="products__priceholder">
<p class="products__price"><strong>86 590</strong> руб.</p>
<small class="products__id">ID. 10906</small>
</div>
<p class="exist">В наличии</p>
<div class="products__buttonholder">
<a href="#" data-reveal-id="buyModal" class="button button-buy-modal">Купить</a>
</div>
</div>
</div>
然后使用jQuery:
var title = $('#div1 .products__title').html();
/// OR
var title = $('.products__item:eq(0) .products__title').html();
var price = $('#div1 .products__price').html();
/// OR
var price = $('.products__item:eq(0) .products__price').html();
price = price.replace(/<[^>]+>/g, ""); // regex to remove tags
编辑
如果你想使用.closest(),你不需要.find()
$("a.button-buy-modal").click(function () {
$(this).closest('.products__priceholder').addClass('test1');
});
谢谢大家的建议,我找到了一个解决方案:
$("a.button-buy-modal").click(function () {
var prTitle = $(this).parent().parent().children('.products__title').html();
var prPrice = $(this).parent().parent().children('.products__priceholder').children('.products__price').children('strong').html();
var prId = $(this).parent().parent().children('.products__priceholder').children('.products__id').html();
var prImage = $(this).parent().parent().parent().children('.products__imageholder').children('.products__thumbnail').attr('src');
console.log(prTitle);
console.log(prPrice);
console.log(prId);
console.log(prImage);
})
相关文章:
- 如何在jQuery中获取元素的形式
- 通过id和class属性获取元素
- Javascript:Can't使用getElementById获取元素
- 使用jquery.html()获取元素本身的html
- 如何从iframe内部获取元素
- Javascript函数获取元素内容
- 使用模板后,使用Angular获取元素属性值
- 获取元素类名的部分
- 从JSON.parse获取元素
- 获取元素内部的缩写
- 在不引用文档的情况下使用AngularJS获取元素
- 如何获取元素's的title属性
- 使用Javascript,获取元素的方法是什么,基于打开和关闭标记之间的文本
- Javascript获取元素背景图像,但消除了“;url()”;
- Javascript:按类获取元素,但跳过某个类
- 当display:none时,getElementById()无法获取元素
- 我的ID获取元素不起作用
- 如何获取元素:使用javascript和CORS悬停背景
- 能够获取元素长度,但不能获取内部 HTML
- 如何使用jQuery在模板kendo UI中获取元素类