这是我的代码为什么它不工作?
<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(); }); });