2012-09-01 28 views
0

如何選擇標題,但沒有字幕的位置:CSS或jQuery選擇排除子

<h1 id="banner"> 
This is the Title 
<span id="subtitle">and the subtitle</span> 
</h1> 

我想這樣做

h1 < span#subtitle {font-weight:bold} 

h1:not(#subtitle) {font-weight:bold} 
+2

如果您將'h1'改爲粗體,您需要將'span'更改爲正常,因爲子元素正在從父母那裏盜取css – Peter

+0

子選擇符是'''而不是'<' – Oriol

+0

您是否想要文本'這是標題'? –

回答

2

只提取標題文本,而不是字幕,使用以下命令:

var text = $("#banner").contents().filter(function() { 
    if (this.nodeType == 3) { // Text Node 
    return this; 
    }   
})[0]; 

DEMO

+0

範圍以外很棒,這就是我所需要的,謝謝! – nathanbweb

2

http://jsfiddle.net/PhSBE/

只需使用

#banner{font-weight:bold} /* Title's rules */ 
#banner>#subtitle{font-weight:normal} /* Subtitle's rules */ 

編輯:

如果要提取文本,你可以看到https://stackoverflow.com/a/12135377/1529630

這是從一個元素中提取文本,忽略HTML元素的功能。

+0

好的謝謝。如果我想在jQuery中使用h1的值(沒有跨度)作爲變量呢?也許一個新的問題.. – nathanbweb

+0

@nathanbweb你是什麼意思與「價值」。 CSS值? 'el.nodeValue'? – Oriol

+0

對不起,我的意思是h1內的文字,但在 – nathanbweb

1

如果你改變h1大膽,你需要改變span正常,因爲子元素是從父母

inherting CSS如果你想使某些屬性This is the Title只需要再拍<span>它。