温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jQuery中怎么设置特殊属性

发布时间:2021-07-23 15:07:21 来源:亿速云 阅读:116 作者:Leah 栏目:web开发

本篇文章给大家分享的是有关jQuery中怎么设置特殊属性,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

jQuery的属性

使用attr()方法读取或设置元素的属性,对于jQuery没有封装的属性(所有浏览器没有差异的属性)用attr进行操作

使用removeAttr删除属性。删除的属性在源代码中看不到,这是和清空属性的区别。attr('name','')

示例:操作属性

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>jQuery测试</title>
    <style type="text/css">
        .cls{
            background-color: fuchsia;
        }
    </style>
    <script type="text/javascript" src="js/jquery-1.12.3.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#btnAdd').click(function(){
                $('#dv').attr('class','cls');
            });
            $('#btnClear').click(function(){
                $('#dv').attr('class','');//属性还有,值没了
            });
            $('#btnRemove').click(function(){
                $('#dv').removeAttr('class');//属性没有了。有属性就要占用内存。
            });
        });
    </script>
</head>
<body>
    <input type="button" id="btnAdd" value="添加样式"/>
    <input type="button" id="btnClear" value="清空样式"/>
    <input type="button" id="btnRemove" value="移除样式"/>
    <div id="dv" ></div>
</body>
</html>

效果图

jQuery中怎么设置特殊属性

示例:全选不选反选

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>jQuery测试</title>
    <style type="text/css">
        .cls{
            background-color: fuchsia;
        }
    </style>
    <script type="text/javascript" src="js/jquery-1.12.3.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#btnAll').click(function(){
                $(':checkbox').prop("checked",true);
            });
            $('#btnNone').click(function(){
                $(':checkbox').prop("checked",false);
            });
            $('#btnReverse').click(function(){
                $(':checkbox').each(function (k,v) {
                    $(v).prop("checked",!$(v).is(":checked"));
                });
            });
        });
    </script>
</head>
<body>
    <input type="button" id="btnAll" value="全选"/>
    <input type="button" id="btnNone" value="不选"/>
    <input type="button" id="btnReverse" value="反选"/>
    <input id="ch_swimming" type="checkbox"/><label for="ch_swimming">游泳</label>
    <input id="ch_basketball" type="checkbox"/><label for="ch_swimming">篮球</label>
    <input id="ch_football" type="checkbox"/><label for="ch_swimming">足球</label>
</body>
</html>

效果图

jQuery中怎么设置特殊属性

RadioButton操作

取得RadioButton的选中值,被选中的radio只有一个值,所以直接用val()

显示单选按钮中哪些内容被选中了,注意多个单选按钮不在同一组中

设置RadioButton的选中值

.attr('checked',true);

$('input[name=gender]').val(["女"]); 或 $(':radio[name=gender]').val(["女"]);

注意:val中参数的[]不能省略,val()的参数必须是一个数组

以上就是jQuery中怎么设置特殊属性,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI