登录 主页

jQuery 的$(":button")

2023-09-15 03:12PM

参考:https://www.runoob.com/jquery/jquery-selectors.html

$(":button") 为 jQuery 中表单选择器,目的是选择所有的按钮,所以会找到 <input><button> 元素;

$("button") 则为基本选择器,目的是选择为 <button> 的标签。

: 即为 jQuery 的过滤选择器,语法类似于 css 中的伪类选择器;其过滤选择器大概可以分为基本过滤(p:first 之类)、内容过滤(:empty)、子元素过滤(:first-child)和属性过滤 [href] 选择器。

通过 $(":button") 可以选取所有 type="button" 的 <input> 元素 和 <button> 元素,如果去掉冒号,$("button")只能获取 <button> 元素。

eg:

<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
    $(document).ready(function(){
        $("#test1").click(function(){
            $("button").hide();
        });
        $("#test2").click(function(){
            $(":button").hide();
        });
    });
</script>
</head>
<body>
    <p id="test1">点进这里测试  <b>button</b></p>
    <p id="test2">点进这里测试 <b>:button</b></p>
    <button>Button 按钮</button>
    <input type="button" value="Input 按钮">
</body>
</html>

点击 button 可以隐藏 Button按钮

点击 :button 可以隐藏 Button按钮 和 Input 按钮

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论