这是我的代码。为什么不起作用?
<Script> $('#colorselector').change(function() { $('.colors').hide(); $('#' + $(this).val()).show(); }); </Script> <Select id="colorselector"> <option value="red">Red</option> <option value="yellow">Yellow</option> <option value="blue">Blue</option> </Select> <div id="red" class="colors" style="display:none"> .... </div> <div id="yellow" class="colors" style="display:none"> ... </div> <div id="blue" class="colors" style="display:none"> ... </div>
您正在加载DOM之前运行代码。
尝试这个:
现场示例:
$(function() { // Makes sure the code contained doesn't run until // all the DOM elements have loaded $('#colorselector').change(function(){ $('.colors').hide(); $('#' + $(this).val()).show(); }); });