2014-02-21 38 views
-2
var machine = $('h1').contents(':not(small, a)').text(); 
console.log(machine); 

<h1>Title <small>subheading</small> <a href="#"></a></h1> 

我試圖讓這Title使用上述jQuery的,但當然這console.logs一個空行中顯示的文本。基本上排除了smalla標籤的jQuery選擇文本排除子

jsFiddle

+0

? –

+2

我在小提琴中刪除了'.modal-left',它工作正常:[FIDDLE LINK](http://jsfiddle.net/5X8N8/6/) – matewka

+0

它的工作..... http://jsfiddle.net/5X8N8/13/ – Jai

回答

1
var machine = $("h1") 
.clone() //clone the element 
.children() //select all the children 
.remove() //remove all the children 
.end() //again go back to selected element 
.text(); 

alert(machine); 
要排除小標籤