Javascript Can';无法获取span属性的值

Javascript Can';无法获取span属性的值,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我想获取div中span属性的值。该div是LI属性的一部分。li属性是用javascript创建的(下面是完整代码)。这是我的html: <ul name="products"> <li class="product" name="product2"> <input type="hidden" name="p2" value="Boter_250g/3/9.00"> <div class="product-image"></div>

我想获取div中span属性的值。该div是LI属性的一部分。li属性是用javascript创建的(下面是完整代码)。这是我的html:

<ul name="products">
<li class="product" name="product2">
<input type="hidden" name="p2" value="Boter_250g/3/9.00">
<div class="product-image"></div>
<div class="product-details">
<a style="color: black;">Boter 250g</a>
<span class="price">€ 9.00</span>
<div class="quantity">
</div>
</div>
</li>
</ul>

总是给出“0”

js:

函数addProduct(){
var产品=产品[类型];
var productname=product.replace(/\ug/g,“”);
var productAdded=$('
  • '+ '' + '' + '' + '' + '' + “
  • ”); cartList.prepend(productAdded); }
    既然您正在使用jquery,为什么不使用:

    $(".price").text();
    

    这里有一些无效的假设

    • li不是一个属性,而是一个HTML元素
    • 您可以使用
      $('yourElement').attr(“”)
    拨弄

    更新: 根据此
    获取跨度值,然后单击
    ing元素

    $('li.product').click(function(){ alert($(this).find('.product-details span.price').html().replace('€', ''))})
    

    更新: 使用删除功能

     <a href="#0" class="delete-item" data-i = ' + i + '>
     $('.delete-item').click(function(){
     var i = $(this).data('i');
      var price = $('li.product[name="product'+i+'"] .product-details span.price').html().replace('$', '');
     })
    
    
    $('.delete item')。单击(函数(){
    var i=$(this.data('i');
    var price=$('li.product[name=“product'+i+'”)。产品详细信息span.price').html()。替换('$','');
    })
    
    我加了一把小提琴。请检查它是否适合你

     $('body').on('click', '.delete', function(e) {
       e.preventDefault();
       var text = $(this).closest('li.product').find('span.price').text()
       console.log(text.replace('€', ''))
     })
    

    您的代码中有错误
    应该是
    productQuantity=Number($(this).find('.quantity').find('input').val())@乔纳森:是的,我刚刚写好了,你能做一把小提琴吗?使用值您在这里调用此
    productToPrice=Number($(this).find('.price').text().replace('€'),'')  $('li.product[name="product'+i+'"] .product-details span.price').html().replace('$', '')
    
    $('li.product').click(function(){ alert($(this).find('.product-details span.price').html().replace('€', ''))})
    
     <a href="#0" class="delete-item" data-i = ' + i + '>
     $('.delete-item').click(function(){
     var i = $(this).data('i');
      var price = $('li.product[name="product'+i+'"] .product-details span.price').html().replace('$', '');
     })
    
     $('body').on('click', '.delete', function(e) {
       e.preventDefault();
       var text = $(this).closest('li.product').find('span.price').text()
       console.log(text.replace('€', ''))
     })