{"id":2829,"date":"2012-12-11T18:27:05","date_gmt":"2012-12-11T10:27:05","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=2829"},"modified":"2018-07-02T11:51:08","modified_gmt":"2018-07-02T03:51:08","slug":"js-get-html5-boolean-attribute-property-required","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2012\/12\/js-get-html5-boolean-attribute-property-required\/","title":{"rendered":"HTML5 Boolean\u7c7b\u578b\u5c5e\u6027(\u5982required)\u503c\u7684JS\u83b7\u53d6"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/\">http:\/\/www.zhangxinxu.com<\/a><br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=2829\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=2829<\/a><\/p>\n<p>\u6211\u4ecd\u5728\u4e0b\u68cb\uff0c\u6211\u4e0d\u53eb\u5c24\u52c7\u3002<\/p>\n<p>\u672c\u6587\u6700\u6700\u5173\u952e\u5b57\u2014\u2014<strong>\u517c\u5bb9\u6027<\/strong>\u3002<\/p>\n<p>\u7bc7\u7bc7\u6587\u7ae0\u50cf\u8001\u592a\u592a\u88f9\u811a\u5e03\u4e00\u6837\uff0c\u81ea\u5df1\u4e5f\u53d7\u4e0d\u4e86\uff0c\u7b80\u6d01\u7b80\u6d01\u3002<\/p>\n<h3>\u4e00\u3001\u5355\u5200\u76f4\u5165\uff0c\u5f00\u95e8\u89c1\u5c71<\/h3>\n<div class=\"zxx_code\">\n<pre>&lt;input required \/>\n&lt;input \/><\/pre>\n<\/div>\n<p>\u5206\u522b\u4f7f\u7528:<br \/>\njQuery1.4.4 <code>attr(\"required\")<\/code>,<br \/>\njQuery1.6.3 <code>attr(\"required\")<\/code>,<br \/>\njQuery1.8.3 <code>prop(\"required\")<\/code>,<br \/>\nMooTools1.4 <code>getProperty(\"required\")<\/code>, \u4ee5\u53ca<br \/>\n\u539f\u751fJS <code>getAttribute(\"required\")<\/code>\u7684\u8fd4\u56de\u503c\u662f\uff1f\uff1f<\/p>\n<p>\u7ed3\u679c\u89c1\u4e0b\u8868\uff08<span style=\"background-color:#f30;\">&nbsp;&nbsp;&nbsp;<\/span>\u8868\u793abug, <span style=\"background-color:#f90;\">&nbsp;&nbsp;&nbsp;<\/span>\u8868\u793a\u4e0d\u51c6\u786e\uff0c\u4e0b\u540c\uff09\uff1a<\/p>\n<div style=\"width:100%; overflow:auto;\">\n<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" class=\"params_table\">\n<tr>\n<th scope=\"col\" style=\"color:#999;\"><em>required\/\u7a7a<\/em><\/th>\n<th scope=\"col\">jQuery 1.4.4 Attr() &#8211; <a href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/jquery-1-4-attr-required.html\">demo<\/a><\/th>\n<th scope=\"col\">jQuery 1.6.3 Attr() &#8211; <a href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/jquery-1-6-attr-required.html\">demo<\/a><\/th>\n<th scope=\"col\">jQuery 1.8.3 Prop() &#8211; <a href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/jquery-1-8-prop-required.html\">demo<\/a><\/th>\n<th scope=\"col\">MooTools 1.4 getProperty() &#8211; <a href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/mootools-1-4-getproperty-required.html\">demo<\/a><\/th>\n<th scope=\"col\">JS getAttribute() &#8211; <a href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/js-getattribute-required.html\">demo<\/a><\/th>\n<\/tr>\n<tr>\n<th scope=\"row\">IE6<\/th>\n<td>\u7a7a\u5b57\u7b26\u4e32\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/undefined<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/null<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/null<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE10\u2198IE7| \u540c\u73af\u5883\u4e0b\u641c\u72d7\u517c\u5bb9<\/th>\n<td><span style=\"background-color:#f30;\">undefined<\/span>\/undefined<\/td>\n<td>required\/<span style=\"background-color:#f30;\">required<\/span><\/td>\n<td><span style=\"background-color:#f30;\">undefined<\/span>\/undefined<\/td>\n<td><span style=\"background-color:#f30;\">-1<\/span>\/null<\/td>\n<td><span style=\"background-color:#f30;\">null<\/span>\/null<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE9\u2198IE7|IE8\u2198IE7 |ieTester IE7<\/th>\n<td>\u7a7a\u5b57\u7b26\u4e32\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/undefined<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/null<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/null<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE10\u2198IE8<\/th>\n<td>required\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td><span style=\"background-color:#f30;\">undefined<\/span>\/undefined<\/td>\n<td><span style=\"background-color:#f90;\">required<\/span>\/null<\/td>\n<td><span style=\"background-color:#f90;\">required<\/span>\/<span style=\"background-color:#f30;\">\u7a7a\u5b57\u7b26\u4e32<\/span><\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE8|IE9\u2198IE8| ieTester IE8<\/th>\n<td>required\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/undefined<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/null<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/null<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE9 All<\/th>\n<td>\u7a7a\u5b57\u7b26\u4e32\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td><span style=\"background-color:#f30;\">undefined<\/span>\/undefined<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/null<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/null<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE10|FireFox17| Chrome21|Opera12<\/th>\n<td>true\/false <\/td>\n<td>required\/undefined<\/td>\n<td>true\/false<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/null<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32\/null<\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p style=\"color:#999; font-size:12px;\">\u6ce8\uff1a1. IE10\u2198IE7\u8868\u793aIE10\u6d4f\u89c8\u5668\u4e0bIE7\u6a21\u5f0f\u3002<br \/>\n<img decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201212\/2012-12-11_160848.png\" \/><\/p>\n<p>2. \u6700\u65b0\u7684jQuery 1.8.3\u4e0e1.6\u7248\u672c\u6d4b\u8bd5\u7ed3\u679c\u4e00\u81f4\uff0c\u8fd9\u91cc\u672a\u5c55\u793a\uff0c\u53ef\u4ee5<a href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/jquery-1-8-attr-required.html\">\u70b9\u51fb\u8fd9\u91cc<\/a>\u67e5\u770b\u3002<br \/>\n3. jQuery prop()\u65b9\u6cd5\u66f4\u60e8\uff01\u751a\u81f3\u539f\u751f\u7684IE9\u6d4f\u89c8\u5668\u90fd\u5927bug\uff01<\/p>\n<p>\u8868\u683c\u5982\u67d3\u574a\uff0c\u53ef\u89c1\u517c\u5bb9\u6027\u60e8\u4e0d\u5fcd\u7779\uff01<\/p>\n<p>\u5c24\u5176<span style=\"border:1px solid #f30;\">IE10\u6d4f\u89c8\u5668\u4e0b\uff0cIE7\/IE8\u6a21\u5f0f<\/span>\uff0c\u7c7b\u4f3crequired\u5c5e\u6027\u503c\u83b7\u53d6\uff0c\u5b58\u5728N\u591a\u4e4b\u524d\u4ece\u672a\u6709\u8fc7\u7684\u6bc1\u706d\u6253\u51fb\u3002<\/p>\n<p>\u8fdejQuery\u5927\u795e\u4e5f\u80cc\u540e\u4e2d\u4e86\u4e00\u67aa\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"jQuery\u8868\u793a\uff0c\u6211\u670d\u4e86\uff0cIE10\u2198IE7 required \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201212\/2012-12-11_161829.png\" title=\"jQuery\u8868\u793a\uff0c\u6211\u670d\u4e86\uff0cIE10\u2198IE7 required\" class=\"alignnone\" width=\"225\" height=\"142\" \/><\/p>\n<h3>\u4e8c\u3001\u6362\u4e2a\u5f62\u5f0f\uff0c\u5bfb\u627e\u5b89\u6170<\/h3>\n<p>\u6211\u5c31\u7422\u78e8\uff0c\u662f\u4e0d\u662f\u56e0\u4e3a\u662fHTML5\u7684Boolean\u4e66\u5199\u5f62\u5f0f\u7684\u539f\u56e0\uff0c\u4e8e\u662f\uff0c\u6539\u6210\u4e86\u5982\u4e0b\u7ee7\u7eed\u6d4b\u8bd5\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;input required=\"required\" \/><\/pre>\n<\/div>\n<p>\u7ed3\u679c\u89c1\u4e0b\u8868\uff1a<\/p>\n<div style=\"width:100%; overflow:auto;\">\n<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" class=\"params_table\">\n<tr>\n<th scope=\"col\"><em style=\"color:#999;\">required=required<\/em> &#8211; <a href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/required-required.html\">demo<\/a><\/th>\n<th scope=\"col\">jQuery 1.4.4 Attr()<\/th>\n<th scope=\"col\">jQuery 1.6.3 Attr()<\/th>\n<th scope=\"col\">MooTools 1.4 getProperty()<\/th>\n<th scope=\"col\">JS getAttribute()<\/th>\n<\/tr>\n<tr>\n<th scope=\"row\">IE6<\/th>\n<td>required<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE10\u2198IE7| \u540c\u73af\u5883\u4e0b\u641c\u72d7\u517c\u5bb9<\/th>\n<td><span style=\"background-color:#f30;\">undefined<\/span><\/td>\n<td>required<\/td>\n<td><span style=\"background-color:#f30;\">-1<\/span><\/td>\n<td><span style=\"background-color:#f30;\">null<\/span><\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE9\u2198IE7|IE8\u2198IE7 |ieTester IE7<\/th>\n<td>required<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE10\u2198IE8<\/th>\n<td>required<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE8|IE9\u2198IE8 |ieTester IE8<\/th>\n<td>required<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE9 All<\/th>\n<td>required<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE10|FireFox17 |Chrome21|Opera12<\/th>\n<td>true<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<td>required<\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>\u54e6\uff0c\u5356\u7cd5\u7684\uff01IE10\u4e0bIE7\u6a21\u5f0f\u4f9d\u7136\u662f<img decoding=\"async\" src=\"http:\/\/mat1.gtimg.com\/www\/mb\/images\/face\/59.gif\" \/>\uff0c\u7ed3\u679c\u4e00\u6a21\u4e00\u6837\uff0c\u770b\u6765\u4e0e\u4e66\u5199\u5f62\u5f0f\u65e0\u5173~~<\/p>\n<p>\u5199\u63d2\u4ef6\u554a\u4ec0\u4e48\u7684\uff0c\u80af\u5b9a\u8981\u5411\u4e0b\u517c\u5bb9\u7684\uff0c\u56e0\u6b64\uff0c\u5373\u4f7f1.6.x\u6709\u6a21\u6709\u6837\uff0c\u7531\u4e8e1.4.x\u62d6\u4e86\u540e\u817f\uff0c\u4e5f\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528attr()\u83b7\u53d6\uff0c\u5509\uff0cjQuery\u6253\u9171\u6cb9\u4e86\uff01\uff01<\/p>\n<p>jQuery\u9171\uff0c\u4e0d\u8981\u6851\u5fc3\uff1b\u770bMooTools\u541b\u4e5f\u6302\u5f69\u966a\u7740\u4f60\u561e\uff01<br \/>\n<img decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201212\/2012-12-11_163244.png\" \/><\/p>\n<h3>\u4e09\u3001\u81ea\u5df1\u52a8\u624b\uff0c\u517c\u5bb9\u5904\u7406<\/h3>\n<p>\u4ee5\u53caJS\u6846\u67b6\u4e2d\u81ea\u5e26\u65b9\u6cd5\u6216\u539f\u751f\u65b9\u6cd5\u4e0d\u80fd\u201c\u4e00\u65b9\u901a\u884c\u201d\uff0c\u90a3\u5c31\u5728\u5176\u57fa\u7840\u4e0a\uff0c\u5bf9\u7167\u6d4b\u8bd5\u6570\u636e\uff0c\u81ea\u5df1\u52a8\u624b\uff0c\u4e30\u8863\u8db3\u98df\u3002<\/p>\n<p><strong>jQuery\u4e0b\u7684\u5904\u7406<\/strong><\/p>\n<div class=\"zxx_code\">\n<pre>(function($, undefined) {\n    $.fn.isRequired = function() {\n        var required;\n        if (document.querySelector) {\n            required = $(this).attr(\"required\");\n            if (required === undefined || required === false) {\n                return undefined;\n            }\n            return \"required\";\n        } else {\n            <span style=\"color:green;\">\/\/ IE6, IE7<\/span>\n            var outer = $(this).get(0).outerHTML, part = outer.slice(0, outer.search(\/\\\/?['\"]?>(?![^<]*<['\"])\/));\n            return \/\\srequired\\b\/i.test(part)? \"required\": undefined;    \n        }\n    };    \n})(jQuery);<\/pre>\n<\/div>\n<p>\u4e5f\u53ef\u4ee5\u8f7b\u8f7b\u5730\u70b9\u51fb\u8fd9\u91cc\u67e5\u770b\uff1a<a target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/jquery-isRequired.js\">jquery-isRequired.js<\/a><\/p>\n<p>\u4e8e\u662f\uff08\u8fd4\u56de\u503c\u7c7b\u578b\u4e0ejQuery <code>attr()<\/code>\u65b9\u6cd5\u9760\u9f50\uff09\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>console.log($(\"#input\").isRequired());    <span style=\"color:green;\">\/\/ required\u6216undefined<\/span><\/pre>\n<\/div>\n<p><strong>MooTools\u4e0b\u7684\u5904\u7406<\/strong><br \/>\nMooTools\u7684\u5904\u7406\u8981\u7b80\u5355\u7684\u591a\uff0c\u4ed4\u7ec6\u5bf9\u6bd4\u6d4b\u8bd5\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u8ba4\u4e3a\u2014\u2014\u53ea\u8981\u8fd4\u56de\u503c\u4e0d\u662f<code>null<\/code>\uff0c\u5219<code>\"required\"<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"MooTools\u4e0b\u6d4b\u8bd5\u6570\u636e\u4e0d\u652f\u6301\u5747\u4e3anull \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201212\/2012-12-11_165249.png\" title=\"MooTools\u4e0b\u6d4b\u8bd5\u6570\u636e\u4e0d\u652f\u6301\u5747\u4e3anull\" class=\"alignnone\" width=\"276\" height=\"258\" \/><\/p>\n<p>\u4e8e\u662f\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>Element.implement({\n    isRequired: function() {\n        return this.getProperty(\"required\") !== null? \"required\": null;\n    }\n});<\/pre>\n<\/div>\n<p>\u4e5f\u53ef\u4ee5\u8f7b\u8f7b\u5730\u70b9\u51fb\u8fd9\u91cc\u67e5\u770b\uff1a<a target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/mt-isRequired.js\">mt-isRequired.js<\/a><\/p>\n<p>\u4e8e\u662f\uff08\u8fd4\u56de\u503c\u7c7b\u578b\u4e0eMooTools <code>getProperty()<\/code>\u65b9\u6cd5\u9760\u9f50\uff09\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>console.log($(\"input\").isRequired());    <span style=\"color:green;\">\/\/ required\u6216null<\/span><\/pre>\n<\/div>\n<p><strong>\u539f\u751fJS\u4e0b\u7684\u5904\u7406<\/strong><br \/>\n\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var $isRequired = function(ele) {\n    if (!ele || ele.nodeType !== 1) return;\n    var isRequired = null;\n    if (typeof window.screenX === \"number\") {\n        if (typeof ele.getAttribute(\"required\") === \"string\") {\n            isRequired = 'required';    \n        }\n    } else {\n        <span style=\"color:green;\">\/\/ IE6, IE7, IE8<\/span>\n        var outer = ele.outerHTML, part = outer.slice(0, outer.search(\/\\\/?['\"]?>(?![^<]*<['\"])\/));\n        if (\/\\srequired\\b\/i.test(part)) isRequired = 'required';    \n    }\n    return isRequired;\n};<\/pre>\n<\/div>\n<p>\u4e5f\u53ef\u4ee5\u8f7b\u8f7b\u5730\u70b9\u51fb\u8fd9\u91cc\u67e5\u770b\uff1a<a target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/js-isRequired.js\">js-isRequired.js<\/a><\/p>\n<p>\u4e8e\u662f\uff08\u8fd4\u56de\u503c\u7c7b\u578b\u4e0eMooTools <code>getProperty()<\/code>\u65b9\u6cd5\u9760\u9f50\uff09\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>console.log($isRequired(document.getElementById(\"input\")));    <span style=\"color:green;\">\/\/ required\u6216null<\/span><\/pre>\n<\/div>\n<p>\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u5747\u653e\u5728\u4e86\u4e00\u4e2a\u9875\u9762\u4e2d\uff0c\u65b9\u4fbf\u6d4b\u8bd5\uff0c\u5bf9\u6bd4\u67e5\u770b\u3002\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/required-compatible.html\">\u517c\u5bb9\u5904\u7406required\u83b7\u53d6\u65b9\u6cd5\u540edemo<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\u517c\u5bb9\u5904\u7406\u6d4b\u8bd5\u9875\u9762\u7f29\u7565\u56fe \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201212\/2012-12-11_170910.png\" title=\"\u517c\u5bb9\u5904\u7406\u6d4b\u8bd5\u9875\u9762\u7f29\u7565\u56fe\" class=\"alignnone\" width=\"514\" height=\"497\" \/><\/p>\n<p>\u6d4b\u8bd5\u6570\u636e\u53c2\u89c1\u4e0b\u8868\uff1a<\/p>\n<div style=\"width:100%; overflow:auto;\">\n<p><strong>&lt;input required \/>\u4ee5\u53ca&lt;input data-type=required \/>\u6d4b\u8bd5<\/strong><\/p>\n<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" class=\"params_table\">\n<tr>\n<th scope=\"col\"><em style=\"color:#999;\">required\/\u975erequired<\/em> - <a href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/required-compatible.html\">demo<\/a><\/th>\n<th scope=\"col\">jQuery 1.4.4 isRequired()<\/th>\n<th scope=\"col\">jQuery 1.6.3 isRequired()<\/th>\n<th scope=\"col\">MooTools 1.4 isRequired()<\/th>\n<th scope=\"col\">JS $isRequired()<\/th>\n<\/tr>\n<tr>\n<th scope=\"row\">IE6<\/th>\n<td>required\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td>required\/null<\/td>\n<td>required\/null<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE10\u2198IE7| \u540c\u73af\u5883\u4e0b\u641c\u72d7\u517c\u5bb9<\/th>\n<td>required\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td>required\/null<\/td>\n<td>required\/null<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE9\u2198IE7|IE8\u2198IE7 |ieTester IE7<\/th>\n<td>required\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td>required\/null<\/td>\n<td>required\/null<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE10\u2198IE8<\/th>\n<td>required\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td>required\/null<\/td>\n<td>required\/null<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE8|IE9\u2198IE8 |ieTester IE8<\/th>\n<td>required\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td>required\/null<\/td>\n<td>required\/null<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE9 All<\/th>\n<td>required\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td>required\/null<\/td>\n<td>required\/null<\/td>\n<\/tr>\n<tr>\n<th scope=\"row\">IE10|FireFox17| Chrome21|Opera12<\/th>\n<td>required\/undefined<\/td>\n<td>required\/undefined<\/td>\n<td>required\/null<\/td>\n<td>required\/null<\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>\u54e6\u54c8\u54c8\uff0c\u7ec8\u4e8e\u5e72\u51c0\u4e86\u4e00\u628a<img decoding=\"async\" src=\"http:\/\/mat1.gtimg.com\/www\/mb\/images\/face\/28.gif\" \/>\uff01<\/p>\n<h3>\u56db\u3001\u5c0f\u5c0f\u6269\u5c55\uff0c\u4e00\u52b3\u6c38\u9038<\/h3>\n<p>\u4ee5\u4e0a\u7684\u53ea\u662f\u9488\u5bf9<code>required<\/code>\u5224\u65ad\u7684\u65b9\u6cd5\uff0c\u5176\u5b9e\uff0c\u540c\u6837\u7684\u539f\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u5c0f\u5c0f\u6269\u5c55\uff0c\u4ee5\u540e\u7c7b\u4f3c<code>autofocus<\/code>, <code>novalidate<\/code>\u8fd9\u4e9bBoolean\u578b\u5c5e\u6027\uff0c\u90fd\u53ef\u4ee5\u7528\u6765\u9a8c\u8bc1\u5566\uff01<br \/>\n<span style=\"color:#555;\">\/\/zxx: <code>checked<\/code>, <code>disabled<\/code>\u867d\u7136\u4e5f\u662fBoolean\u578b\uff0c\u4f46\u662f\uff0c\u4f4e\u7248\u672cIE\u652f\u6301\u4e4b\uff0c\u56e0\u6b64\uff0c\u4f7f\u7528\u81ea\u5e26\u65b9\u6cd5\u5982<code>prop()<\/code>\u5373\u53ef\u3002<\/span><\/p>\n<p>\u4ee5jQuery\u73af\u5883\u4e3e\u4f8b\uff0c\u6211\u4eec\u6269\u5c55\u4e2a<code>hasProp()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>(function($, undefined) {\n    $.fn.hasProp = function(prop) {\n        if (typeof prop !== \"string\") return undefined; \n        var hasProp = false;\n        if (document.querySelector) {\n            var attrProp = $(this).attr(prop);\n            if (attrProp !== undefined && attrProp !== false) {\n                hasProp = true;\n            }\n        } else {\n            \/\/ IE6, IE7\n            var outer = $(this).get(0).outerHTML, part = outer.slice(0, outer.search(\/\\\/?['\"]?>(?![^<]*<['\"])\/));\n            hasProp = new RegExp(\"\\\\s\" + prop + \"\\\\b\", \"i\").test(part);\n        }\n        return hasProp;\n    };    \n})(jQuery);<\/pre>\n<\/div>\n<p>\u4e8e\u662f\uff0c\u5c31\u53ef\u4ee5\u6d4b\u8bd5\u4e86\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/hasprop-compatible.html\">hasProp()\u6269\u5c55\u65b9\u6cd5\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u7ed3\u679c\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"hasProp\u6269\u5c55\u65b9\u6cd5\u6d4b\u8bd5\u6548\u679c\u622a\u56fe \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201212\/2012-12-11_175936.png\" title=\"hasProp\u6269\u5c55\u65b9\u6cd5\u6d4b\u8bd5\u6548\u679c\u622a\u56fe\" class=\"alignnone\" width=\"470\" height=\"227\" \/><\/p>\n<p>JS\u6587\u4ef6\u53ef\u4ee5\u8f7b\u8f7b\u5730\u70b9\u51fb\u8fd9\u91cc\u67e5\u770b\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201212\/jquery-hasProp.js\" target=\"_blank\">jquery-hasProp.js<\/a><\/p>\n<h3>\u4e94\u3001\u9a71\u8f66\u8d76\u8def\uff0c\u6167\u7136\u79bb\u53bb<\/h3>\n<p>\u4e0d\u5f97\u4e0d\u5410\u69fd\u3002IE10\u6d4f\u89c8\u5668\uff0c\u770b\u4e0a\u53bb\u8fd8\u6709\u70b9\u98ce\u5149\uff0c\u53ef\u4ee5\uff0c\u6216\u8bb8\u6b63\u56e0\u4e3a\u5176\u5927\u5927\u652f\u6301\u4e86HTML5\u7684\u8868\u5355\u76f8\u5173\u7279\u5b9a\uff0c\u5bfc\u81f4\u5176\u5411\u4e0b\u7684\u517c\u5bb9\u6a21\u5f0f\uff08IE7\/IE8\/IE9\uff09\u5728\u5904\u7406\u7684\u65f6\u5019\uff0c\u5077\u5de5\u51cf\u6599\uff08\u76f4\u63a5\u4ece\u6e32\u67d3\u5c42\u9762\u628a\u4e00\u4e9bHTML5\u5c5e\u6027\u7f6e\u4e3a\u666e\u901a\u6216\u602a\u5f02\u8bc6\u522b\u2014\u2014\u957f\u76f8\u6b63\u5e38\uff0c\u5185\u90e8\u8150\u8d25\uff09\uff0c\u4ee5\u81f3\u4e8e\u5728\u4e00\u4e9b\u5c5e\u6027\u83b7\u53d6\u7684\u65f6\u5019\uff0cbug\u6a2a\u751f\u3002\u672c\u6587\u4ecb\u7ecd\u7684bug\u8fd8\u662f\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u624b\u6bb5\u4fee\u590d\u7684\u3002\u4e0b\u4e00\u7bc7\u6587\u7ae0\u8981\u4ecb\u7ecd\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u8bf4\u662f\u53eb\u5929\u5929\u4e0d\u5e94\u53eb\u5730\u5730\u4e0d\u7075\u3002<\/p>\n<p>\u672c\u6587\u7684\u6d4b\u8bd5\u7ed3\u679c\u8981\u611f\u8c22\u80e1\u603b\uff0c\u90d1\u660e\u4e3d\uff0c\u8d75\u5029\u7684\u6d4f\u89c8\u5668\u5927\u529b\u652f\u6301\u3002\u4e0d\u8fc7\uff0c\u8fd8\u6709N\u591a\u5176\u4ed6\u6d4f\u89c8\u5668\uff0cxp\u73af\u5883\u7684\u641c\u72d7\uff0cwin7\u73af\u5883\u9068\u6e38\uff0c360\u4e4b\u6d41\uff0c\u6216\u8005win8\u4e0b\u9762\u7684IE10\u7b49\u7b49\uff1b\u5982\u679c\u6709\u4e0d\u540c\u7684\u6d4b\u8bd5\u7ed3\u679c\uff0c\u6b22\u8fce\uff08\u975e\u5e38\u611f\u8c22\uff01\uff09\u63d0\u51fa\uff01<\/p>\n<p>\u82e6\u903c\u7684\u517c\u5bb9\u6027\u95ee\u9898\uff0csign~<img decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201212\/sign.gif\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u81ea<a href=\"http:\/\/www.zhangxinxu.com\/\">\u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<\/a>[<a href=\"http:\/\/www.zhangxinxu.com\/\">http:\/\/www.zhangxinxu.com<\/a>]<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=2829\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=2829<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HTML5 Boolean\u7c7b\u578b\u5c5e\u6027\uff0c\u770b\u4e0a\u53bb\u8ddf\u666e\u901a\u5c5e\u6027\u6ca1\u4ec0\u4e48\u4e24\u4e2a\uff0c\u6df1\u5165\u5904\u7406\u624d\u53d1\u73b0\u4e8b\u60c5\u6ca1\u6709\u60f3\u8c61\u7684\u90a3\u4e48\u7b80\u5355\uff0c\u5c24\u5176\u6700\u8fd1IE10\u6d4f\u89c8\u5668\u7684\u51fa\u73b0\uff0c\u66f4\u662f\u8840\u96e8\u8165\u98ce\uff0c\u5176\u517c\u5bb9\u6027\u95ee\u9898\u5938\u5f20\u5730\u4ee4\u4eba\u5567\u820c\uff0c\u751a\u81f3\u6700\u65b0\u7248\u672c\u7684jQuery\u90fd\u65e0\u6cd5\u907f\u514d\u5730\u51fa\u73b0\u4e86bug.<\/p>\n<p>\u672c\u6587\u5c31\u5c06\u8be6\u5c3d\u5c55\u793a\u5404\u79cd\u6761\u4ef6\u4e0b\uff0cBoolean\u7c7b\u578b\u5c5e\u6027(\u5982required)\u503c\uff0c\u8ba9\u4f60\u53ef\u4ee5\u72ec\u773c\u9f99\u770b\u544a\u793a\u2014\u2014\u4e00\u76ee\u4e86\u7136\u5730\u77e5\u9053bug\u51fa\u5728\u54ea\u91cc\uff0c\u54ea\u4e9b\u6d4f\u89c8\u5668\u4e0a\u3002\u672c\u6587\u8fd8\u63d0\u4f9b\u5404\u79cd\u5e93\u7684\u7684\u517c\u5bb9\u6027\u4fee\u590d\u65b9\u6848\uff0c\u5e76\u4f5c\u4e86\u7b80\u5355\u6269\u5c55\u3002\u57fa\u672c\u4e0a1~2\u5929\u65f6\u5019\u7684\u6d4b\u8bd5\u4e0e\u5904\u7406\uff0c\u672c\u6587\u7684\u6570\u636e\u76f8\u4fe1\u4f1a\u5bf9\u60a8\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u7684\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[767,768,52,769,14,448,546,140],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/2829"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2829"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/2829\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}