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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM