如何使用jQuery获取<span>的值?
这是基本的。
如何获得上述跨度的“这是我的名字”的价值?
<div id='item1'> <span>This is my name</span> </div> 我认为这应该是一个简单的例子:
 $('#item1 span').text(); 
要么
 $('#item1 span').html(); 
 $("#item1 span").text(); 
假设你打算读取id =“item1”,你需要
 $('#item1 span').text() 
 $('#item1').text(); or $('#item1').html(); 适用于id="item1" 
由于您没有为“item”值提供属性,因此我假定正在使用一个类:
 <div class='item1'> <span>This is my name</span> </div> alert($(".item span").text()); 
 确保你等待DOM加载使用你的代码,在jQuery中你使用ready()函数: 
 <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> 
 在JavaScript中不会使用getelementbyid('item1').innertext ? 
  $('span id').text(); worked with me 
  $('#id span').text()就是答案! 
 $('#item1 span').html(); 它与我的代码工作 
 <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>