2011-06-17 25 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 ]