jquery添加删除html标签属性

转自:http://blog.csdn.net/fengspg/article/details/7085872

区别jquery对象跟dom对象,首先做好好的编程习惯:

一般jquery对象前面加"$":var $sure = $("#sure");
dom对象直接编写:var sure = document.getElementById("sure").value。

jquery给html标签添加属性:$sub.attr("disabled","disabled");
jquery给html标签移除属性:$sub.removeAttr("disabled")。

实例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>jQuery测试</title>
        <script type="text/javascript" src="jquery-1.8.0.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                var $sure = $("#sure");
                var $sub = $("#sub");
                var sure = document.getElementById("sure").value;
                $sure.click(function(){
                    if(!$sure.is(":checked")){
                        $sub.attr("disabled","disabled");
                    }

                    if($sure.is(":checked")){
                        $sub.removeAttr("disabled");
                    }
                })
            })
        </script>
    </head>

    <body>
        <input type="checkbox" id="sure" />同意并接受注册协议
        <input type="button" disabled="disabled" value="提交" id="sub" />
    </body>
</html>

 

posted @ 2013-01-19 14:14  horizon~~~  阅读(6569)  评论(0编辑  收藏  举报