使用jQuery剪辑内容

问题描述:

要获取跨度内的数字,请使用以下代码:

to just get the number inside span, use the code below:

<h3>test <span>inside 123458</span></h3>


alert($('h3 span').text().match(/\d/))
//123458

仅获取内容h3,而不占用内容范围,怎么办?

And to just get the content h3, without taking the span of content, how?

jQuery解决方案

jQuery solution

alert( $('h3').clone().find('span').remove().end().text() );

纯JS解决方案

alert( document.getElementsByTagName('h3')[0].childNodes[0].textContent );

示例: http://jsfiddle.net/2mTLg/1