开发者

selecting un-id spans in a div

My markup is a div with 3 spans inside. How can I read the value of each span with jquery.

<div id="mod">
   <span>first sp开发者_如何学Can<span>
   <span>second span<span>
   <span>third span<span>
</div>

function getVars(){
  var span1 = ;
  var span2 = ;
  var span3 = ;
}


please try this

$('#mod').find('span').text();


        $(document).ready(function(){
            alert($("#mod span:first").text());//First span child only
            alert($("#mod > span").text());// All span children
        });

Note: Make sure to close your span "".


You can also use this

$(document).ready(function () {
      alert($("#mod span:eq(0)").html());
    });


Close the span tags <span> first </span> then it should work, all the answers

another variation

$(document).ready(function () {
    alert($("#mod").children(":first").text());
});


<!DOCTYPE html>
<html>
<head>
  <style>
  span { color:#008; }
  span.sogreen { color:green; font-weight: bolder; }
  </style>
  <script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
  <div>
    <span>John,</span>
    <span>Karl,</span>
    <span>Brandon</span>

  </div>
  <div>
    <span>Glen,</span>
    <span>Tane,</span>
    <span>Ralph</span>

  </div>
<script>
    $("div span:first-child")
        .css("text-decoration", "underline")
        .hover(function () {
              $(this).addClass("sogreen");
            }, function () {
              $(this).removeClass("sogreen");
            });

</script>

</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜