简体   繁体   English

使用jquery获取单击类的子元素的值

[英]Get Values of sub elements of clicked class using jquery

I have a class amt and when that class is clicked I want to get the values of the clicked <h6> , <span> and <label> tags. 我有一个类amt ,当点击该类时,我想获得点击的<h6><span><label>标签的值。 How do I do this in jquery? 我如何在jquery中执行此操作? I have already seen a question here Get value of List Item with jQuery but it uses same 我已经在这里看到一个问题用jQuery获取List Item的值,但它使用相同的

  • under tag but i have to get different elemet value under same tag 在标签下但我必须在同一标签下获得不同的elemet值

     <li class="amt" id="diecut_am1"> <h6>50</h6> <span>$59.00</span> <label>$51.30</label> </li> <li class="amt" id="diecut_am2"> <h6>100</h6> <span>$68.00</span> <label>$61.20</label> </li> 
  • Try this 试试这个

    $(".amt").click(function() {
        var elem1 = $(this).find("h6").html();
        var elem2 = $(this).find("span").html();
        var elem3 = $(this).find("label").html();
    
        alert(elem1);
        alert(elem2);
        alert(elem3);
    });
    

    https://jsfiddle.net/kLe5kLc3/1/ https://jsfiddle.net/kLe5kLc3/1/

    You could do something like this: 你可以这样做:

    $( document ).ready(function() {
        $('.amt').on("click", function() {
           var h6 = $(this).find('h6').text();
           var span = $(this).find('span').text();
           var label = $(this).find('label').text();
        });
    });
    

    Demo: https://jsfiddle.net/12q12k52/ 演示: https//jsfiddle.net/12q12k52/

    here's the JS way : 这是JS的方式:

     var amt = document.querySelectorAll('.amt') //add event listener to all .amt elements var amtArr = [].slice.call(amt) amtArr.forEach(function (x) { x.addEventListener('click', listChilds, true) }); //we retrive the target properties function listChilds(e) { console.log(e.path[1]) //all the children //if you want one in particular it would be console.log(e.target.childNodes[0]) } 
     <li class="amt" id="diecut_am1"> <h6>50</h6> <span>$59.00</span> <label>$51.30</label> </li> <li class="amt" id="diecut_am2"> <h6>100</h6> <span>$68.00</span> <label>$61.20</label> </li> 

    您可以迭代单击元素的元素

    $(this).children()
    

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

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