我对jQuery很陌生,想知道如何在下拉选择菜单中选择一个选项。
更具体地说:您可以在下拉菜单中选择一种颜色,然后单击按钮,背景颜色将变为该颜色。
提前致谢!
这是HTML:
<div id="form-box">
<h1>Funky Background Color Changer</h1>
<p>1. Choose a funky background color:
<select name="bgColor" id="">
<option value="$juicy-cyan">Juicy Cyan</option>
<option value="$purdy-purple">Purdy Purple</option>
</select>
</p>
<p>2. That's it actually! Just click the button.</p><br>
<button class="funk">Let's Funk!</button>
</div>
例如,将ID添加到您h1
和您的颜色(十六进制值)中的选定列表的值上,然后在单击按钮时设置背景:
Html
<div id="form-box">
<h1 id="h1_id">Funky Background Color Changer</h1>
<p>1. Choose a funky background color:
<select name="bgColor" id="bgColor_id">
<option value="#ff0000">Juicy Cyan</option>
<option value="#0066ff">Purdy Purple</option>
</select>
</p>
<p>2. That's it actually! Just click the button.</p><br>
<button class="funk" id="submitButton">Let's Funk!</button>
</div>
jQuery查询
$('#submitButton').on('click', function () {
var color = $("#bgColor_id").val();
$("#h1_id").css('background-color', color);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句