2011-06-17 24 views
0

我有填充,隱藏和顯示基於它是否包含一個ID的一部分div的腳本,然後顯示,包含完整的DIV不工作,唯一的ID傳遞給函數。功能如下:jQuery的隱藏與attributesStartsWith預期

function showPlot(plotId) { 
    // Hide all plots by switching them to a class 
    $('div[id^="plot_"').hide(); //<--Won't hide the divs matching that portion of the Id 
    // Show the selected plot by changing it's class 
    $('#' + plotId).show('fast'); 
} 

的問題是.hide()函數不會隱藏指定的div預期。我可能沒有正確的語法,但我非常肯定根據API是正確的。還有什麼我失蹤?幫助表示讚賞。

+0

是函數正確調用? –

回答

3

你錯過收盤]

$('div[id^="plot_"]') 
      // ^---------was missing 
+0

它可以與'plot_'周圍的引號一起使用嗎?我認爲jQuery會尋找一個帶有id =「」plot _「」'的元素,其中內部引號是實際id的一部分,如果這樣做合理的話。也許不是 –

+1

@Connor:不,jQuery不會查找該ID。事實上,引號是強制性的:http://api.jquery.com/attribute-equals-selector/ –

+0

@Connor:是的,它會這樣工作。對於各種*屬性選擇器的值,引號實際上是強制性的。 – user113716

1

變化

$('div[id^="plot_"').hide(); 

$('div[id^="plot_"]').hide(); // you were missing the last ]