How do I get a value of a using jQuery?

后端 未结 12 1389
青春惊慌失措
青春惊慌失措 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:09
         $('span id').text(); worked with me
    
    0 讨论(0)
  • 2020-11-28 08:09

    You could use id in span directly in your html.

    <span id="span_id">Client</span>
    

    Then your jQuery code would be

    $("#span_id").text();
    

    Some one helped me to check errors and found that he used val() instead of text(), it is not possible to use val() function in span. So

    $("#span_id").val();
    

    will return null.

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

    Since you did not provide an attribute for the 'item' value, I am assuming a class is being used:

    <div class='item1'>
      <span>This is my name</span>
    </div>
    
    alert($(".item span").text());
    

    Make sure you wait for the DOM to load to use your code, in jQuery you use the ready() function for that:

    <html>
     <head>
      <title>jQuery test</title>
      <!-- script that inserts jquery goes here -->
      <script type='text/javascript'>
        $(document).ready(function() { alert($(".item span").text()); });
      </script>
    </head>
    <body>
     <div class='item1'>
       <span>This is my name</span>
     </div>
    </body>
    

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

    $('#item1 span').html(); Its working with my code

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

    Assuming you intended it to read id="item1", you need

    $('#item1 span').text()
    
    0 讨论(0)
  • 2020-11-28 08:22

    In javascript wouldn't you use document.getElementById('item1').innertext?

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