jQuery addClass使用输入选择 [英] jQuery addClass using input select

查看:43
本文介绍了jQuery addClass使用输入选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery的新手.

Im a bit of a jQuery novice.

我在下拉菜单中有5个选项.我想根据选择的选项在div中添加一个类.然后,此类将使用CSS更改内容的布局.

I have 5 options in a drop down menu. I want to add a class in a div depending on which option is selected. This class will then change the layout of the content using CSS.

这是一个有帮助的例子!

Here is an example if it helps!

谢谢

<form>

<select>
<option id="1">layout 1</option>
<option id="2">layout 2</option>
<option id="3" selected>layout 3</option>
<option id="4">layout 4</option>
<option id="5">layout 5</option>
</select>

<div class="3">styled content</div>

</form>

推荐答案

您可以使用".attr()"来设置div onchange的class属性.最好先将选项 id 更改为 value .然后:

You can use the ".attr()" to set the class attribute of the div onchange. You're best to change the option id to value first. then:

$("select").change(function() {
   $("div").attr("class", $(this).val());
});

(编辑)将其更改为:

$("select#np_blog_layout").change(function() {
   $("div#changebox").attr("class", $(this).val());
});

这篇关于jQuery addClass使用输入选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆