How to get a bit of text using jQuery
I have the following scenario where I need to get text from a specific id area, for instance, in this case I'd like to get the name of th开发者_JAVA百科e person. I already know the id i.e. booking_11 as I've got this from the onClick:
<li id="booking_11">Name of person <span style="float: right;"><a href="#" id="11" class="dialog">View</a> | <a class="remove" id="rmCandidate_11" href="#">Remove</a></span></li>
To get only that "Name of person"
text node you can do this (without changing your markup):
var name = $("#booking_11").contents().filter(function() {
return this.nodeType == 3;
}).text();
You can test it out here (broken up above to fit SO cleanly, it can be a one-liner).
Make a div/span like this:
<span class="test">Name of person</span> View | Remove
And then:
jQuery('#booking_11 > .test').text();
$('booking_11').text();
精彩评论