Possible Duplicate:
How do I select text nodes with jQuery?
What is the correct way to deal with texts outside the tags
I tried all the methods, but all failed
And I experimented with this
<script>
$(function(){
var A = $(".A").text();
var B = $(".B").text();
var C = $(".C").text();
var D = $(".D").text();
$("#BD").text(A+B+C+D);
});
</script>
<body>
<div>
<span class="A"></span>6
<span class="B"></span>8
<span class="C"></span>3
<span class="D"></span>9
</div>
<div id="BD"></div>
</body>
As well as this
<script>
$(function(){
var A = $(".A").text();
var B = $(".B").text();
var C = $(".C").text();
var D = $(".D").text();
alert(A+B+C+D);
});
</script>
But they all didn’t work
How can I collect the numbers correctly?!
You could clone the div and remove the span elements inside so then retrieve the div text with
.text(), just do:See working demo