jQuery选择器之表单元素选择器

jQuery中表单选择器的用法实例如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="jquery-3.3.1.js"></script>
</head>

<body>
    <h2>子元素筛选选择器</h2>
    <h3>input、text、password、radio、checkbox</h3>
    <h3>submit、image、reset、button、file</h3>
    <div class="left first-div">
        <form>
            <input type="text" value="text类型"/>
            <input type="password" value="password"/>
            <input type="radio"/> 
            <input type="checkbox"/>
            <input type="submit" />
            <input type="image" />
            <input type="reset" />
            <input type="button" value="Button" />
            <input type="file" />
        </form>
    </div>

    <script type="text/javascript">
        //查找所有 input, textarea, select 和 button 元素
        //:input 选择器基本上选择所有表单控件
        $(":input").css("border", "1px groove red"); 
    </script>

    <script type="text/javascript">
        //匹配所有input元素中类型为text的input元素
        $("input:text").css("background", "#A2CD5A");
    </script>

    <script type="text/javascript">
        //匹配所有input元素中类型为password的input元素
        $("input:password").css("background", "yellow");
    </script>

    <script type="text/javascript">
        //匹配所有input元素中的单选按钮,并选中
        $("input:radio").attr('checked','true');
    </script>

    <script type="text/javascript">
        //匹配所有input元素中的复选按钮,并选中
        $("input:checkbox").attr('checked','true'); 
    </script>

    <script type="text/javascript">
        //匹配所有input元素中的提交的按钮,修改背景颜色
        $("input:submit").css("background", "#C6E2FF");
    </script>

    <script type="text/javascript">
        //匹配所有input元素中的图像类型的元素,修改背景颜色
        $("input:image").css("background", "#F4A460");
    </script>

    <script type="text/javascript">
        //匹配所有input元素中类型为按钮的元素
        $("input:button").css("background", "red");
    </script>

    <script type="text/javascript">
        //匹配所有input元素中类型为file的元素
        $("input:file").css("background", "#CD1076");
    </script>

</body>
</html>

浏览器中运行结果如下:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值