How do I get a value of a using jQuery?

后端 未结 12 1390
青春惊慌失措
青春惊慌失措 2020-11-28 07:53

This is basic.

How do I get the value \'This is my name\' of the above span?

This is my name
相关标签:
12条回答
  • 2020-11-28 08:23

    $('#item1').text(); or $('#item1').html(); works fine for id="item1"

    0 讨论(0)
  • 2020-11-28 08:23

    VERY IMPORTANT Additional info on difference between .text() and .html():

    If your selector selects more than one item, e.g you have two spans like so <span class="foo">bar1</span> <span class="foo">bar2</span> ,

    then

    $('.foo').text(); appends the two texts and give you that; whereas

    $('.foo').html(); gives you only one of those.

    0 讨论(0)
  • 2020-11-28 08:25

    I think this should be a simple example:

    $('#item1 span').text();
    

    or

    $('#item1 span').html();
    
    0 讨论(0)
  • 2020-11-28 08:25
    $("#item1 span").text();
    
    0 讨论(0)
  • 2020-11-28 08:31
    <script>
        $(document).ready(function () {
    
                $.each($(".classBalence").find("span"), function () {
                if ($(this).text() >1) {
                    $(this).css("color", "green")
    
                }
                if ($(this).text() < 1) {
                    $(this).css("color", "red")
                    $(this).css("font-weight", "bold")
                }
            });
    
        });
        </script>
    
    0 讨论(0)
  • 2020-11-28 08:33

    $('#id span').text() is the answer!

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