How to get value by class name in JavaScript or jquery?

前端 未结 3 883
-上瘾入骨i
-上瘾入骨i 2020-12-16 23:23

i wan to retrive all the value of this code using class name ..is it possible in jquery . i want to retrive only the text within a div or number of div may be change the nex

相关标签:
3条回答
  • 2020-12-16 23:55

    Try this:

    $(document).ready(function(){
        var yourArray = [];
        $("span.HOEnZb").find("div").each(function(){
            if(($.trim($(this).text()).length>0)){
             yourArray.push($(this).text());
            }
        });
    });
    

    DEMO

    0 讨论(0)
  • 2020-12-16 23:58

    If you get the the text inside the element use

    Text()

    $(".element-classname").text();
    

    In your code:

    $('.HOEnZb').text();
    

    if you want get all the data including html Tags use:

    html()

     $(".element-classname").html();
    

    In your code:

    $('.HOEnZb').html();
    

    Hope it helps:)

    0 讨论(0)
  • 2020-12-16 23:58

    Without jQuery:

    textContent:

    var text = document.querySelector('.someClassname').textContent;
    

    Markup:

    var text = document.querySelector('.someClassname').innerHTML;
    

    Markup including the matched element:

    var text = document.querySelector('.someClassname').outerHTML;
    

    though outerHTML may not be supported by all browsers of interest and document.querySelector requires IE 8 or higher.

    0 讨论(0)
提交回复
热议问题