{"id":191166,"date":"2023-08-05T20:49:44","date_gmt":"2023-08-05T12:49:44","guid":{"rendered":"https:\/\/www.idc.net\/help\/191166\/"},"modified":"2023-08-05T20:49:44","modified_gmt":"2023-08-05T12:49:44","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91knockout%e6%95%99%e7%a8%8b%ef%bc%9a%e7%bb%91%e5%ae%9a%e8%af%ad%e6%b3%95%ef%bc%881%ef%bc%89","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/191166\/","title":{"rendered":"\u540e\u6d6a\u4e91Knockout\u6559\u7a0b\uff1a\u7ed1\u5b9a\u8bed\u6cd5\uff081\uff09"},"content":{"rendered":"<h2>1&nbsp;&nbsp; visible \u7ed1\u5b9a<\/h2>\n<p><strong>\u76ee\u7684<\/strong><\/p>\n<p>visible\u7ed1\u5b9a\u5230DOM\u5143\u7d20\u4e0a\uff0c\u4f7f\u5f97\u8be5\u5143\u7d20\u7684hidden\u6216visible\u72b6\u6001\u53d6\u51b3\u4e8e\u7ed1\u5b9a\u7684\u503c\u3002<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><\/p>\n<pre><code>&lt;div data-bind=\"visible: shouldShowMessage\"&gt;\n    You will see this message only when \"shouldShowMessage\" holds a true value.\n&lt;\/div&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n    var viewModel = {\n        shouldShowMessage: ko.observable(true) \/\/ Message initially visible\n    };\n    viewModel.shouldShowMessage(false); \/\/ ... now it's hidden\n    viewModel.shouldShowMessage(true); \/\/ ... now it's visible again\n&lt;\/script&gt;<\/code><\/pre>\n<p><strong>\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>\u4e3b\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u5f53\u53c2\u6570\u8bbe\u7f6e\u4e3a\u4e00\u4e2a<strong>\u5047\u503c<\/strong>\u65f6\uff08\u4f8b\u5982\uff1a\u5e03\u5c14\u503cfalse\uff0c \u6570\u5b57\u503c0\uff0c \u6216\u8005null\uff0c \u6216\u8005undefined\uff09 ,\u8be5\u7ed1\u5b9a\u5c06\u8bbe\u7f6e\u8be5\u5143\u7d20\u7684style.display\u503c\u4e3anone\uff0c\u8ba9\u5143\u7d20\u9690\u85cf\u3002\u5b83\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u4f60\u5728CSS\u91cc\u5b9a\u4e49\u7684\u4efb\u4f55display\u6837\u5f0f\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u5f53\u53c2\u6570\u8bbe\u7f6e\u4e3a\u4e00\u4e2a<strong>\u771f\u503c<\/strong>\u65f6\uff08\u4f8b\u5982\uff1a\u5e03\u5c14\u503ctrue\uff0c\u6216\u8005\u975e\u7a7anon-null\u7684\u5bf9\u8c61\u6216\u8005\u6570\u7ec4\uff09 ,\u8be5\u7ed1\u5b9a\u4f1a\u5220\u9664\u8be5\u5143\u7d20\u7684style.display\u503c\uff0c\u8ba9\u5143\u7d20\u53ef\u89c1\u3002\u7136\u540e\u4f60\u5728CSS\u91cc\u81ea\u5b9a\u4e49\u7684display\u6837\u5f0f\u5c06\u4f1a\u81ea\u52a8\u751f\u6548\u3002<\/p>\n<p>&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; \u5982\u679c\u53c2\u6570\u662f\u76d1\u63a7\u5c5e\u6027observable\u7684\uff0c\u90a3\u5143\u7d20\u7684visible\u72b6\u6001\u5c06\u6839\u636e\u53c2\u6570\u503c\u7684\u53d8\u5316\u800c\u53d8\u5316\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u90a3\u5143\u7d20\u7684visible\u72b6\u6001\u5c06\u53ea\u8bbe\u7f6e\u4e00\u6b21\u5e76\u4e14\u4ee5\u540e\u4e0d\u5728\u66f4\u65b0\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>\u5176\u5b83\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u65e0<\/p>\n<p><strong>\u6ce8\uff1a\u4f7f\u7528\u51fd\u6570\u6216\u8005\u8868\u8fbe\u5f0f\u6765\u63a7\u5236\u5143\u7d20\u7684\u53ef\u89c1\u6027<\/strong><\/p>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528JavaScript\u51fd\u6570\u6216\u8005\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570\u3002\u8fd9\u6837\u7684\u8bdd\uff0c\u51fd\u6570\u6216\u8005\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u5c06\u51b3\u5b9a\u662f\u5426\u663e\u793a\/\u9690\u85cf\u8fd9\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>&lt;div data-bind=\"visible: myValues().length &gt; 0\"&gt;\n    You will see this message only when 'myValues' has at least one member.\n&lt;\/div&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n    var viewModel = {\n        myValues: ko.observableArray([]) \/\/ Initially empty, so message hidden\n    };\n    viewModel.myValues.push(\"some value\"); \/\/ Now visible\n&lt;\/script&gt;<\/code><\/pre>\n<p><strong>\u4f9d\u8d56\u6027<\/strong><\/p>\n<p>\u9664KO\u6838\u5fc3\u7c7b\u5e93\u5916\uff0c\u65e0\u4f9d\u8d56\u3002<\/p>\n<h2>2&nbsp;&nbsp; text \u7ed1\u5b9a<\/h2>\n<p><strong>\u76ee\u7684<\/strong><\/p>\n<p>text \u7ed1\u5b9a\u5230DOM\u5143\u7d20\u4e0a\uff0c\u4f7f\u5f97\u8be5\u5143\u7d20\u663e\u793a\u7684\u6587\u672c\u503c\u4e3a\u4f60\u7ed1\u5b9a\u7684\u53c2\u6570\u3002\u8be5\u7ed1\u5b9a\u5728\u663e\u793a\u6216\u8005\u4e0a\u975e\u5e38\u6709\u7528\uff0c\u4f46\u662f\u4f60\u53ef\u4ee5\u7528\u5728\u4efb\u4f55\u5143\u7d20\u4e0a\u3002<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><\/p>\n<pre><code>Today's message is: &lt;span data-bind=\"text: myMessage\"&gt;&lt;\/span&gt; \n\n&lt;script type=\"text\/javascript\"&gt;\n    var viewModel = {\n        myMessage: ko.observable() \/\/ Initially blank\n    };\n    viewModel.myMessage(\"Hello, world!\"); \/\/ Text appears\n&lt;\/script&gt;<\/code><\/pre>\n<p><strong>\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>\u4e3b\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp; KO\u5c06\u53c2\u6570\u503c\u4f1a\u8bbe\u7f6e\u5728\u5143\u7d20\u7684innerText \uff08IE\uff09\u6216textContent\uff08Firefox\u548c\u5176\u5b83\u76f8\u4f3c\u6d4f\u89c8\u5668\uff09\u5c5e\u6027\u4e0a\u3002\u539f\u6765\u7684\u6587\u672c\u5c06\u4f1a\u88ab\u8986\u76d6\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u5982\u679c\u53c2\u6570\u662f\u76d1\u63a7\u5c5e\u6027observable\u7684\uff0c\u90a3\u5143\u7d20\u7684text\u6587\u672c\u5c06\u6839\u636e\u53c2\u6570\u503c\u7684\u53d8\u5316\u800c\u66f4\u65b0\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u90a3\u5143\u7d20\u7684text\u6587\u672c\u5c06\u53ea\u8bbe\u7f6e\u4e00\u6b21\u5e76\u4e14\u4ee5\u540e\u4e0d\u5728\u66f4\u65b0\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u5982\u679c\u4f60\u4f20\u7684\u662f\u4e0d\u662f\u6570\u5b57\u6216\u8005\u5b57\u7b26\u4e32\uff08\u4f8b\u5982\u4e00\u4e2a\u5bf9\u8c61\u6216\u8005\u6570\u7ec4\uff09\uff0c\u90a3\u663e\u793a\u7684\u6587\u672c\u5c06\u662fyourParameter.toString()\u7684\u7b49\u4ef7\u5185\u5bb9\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>\u5176\u5b83\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u65e0<\/p>\n<p><strong>\u6ce81\uff1a\u4f7f\u7528\u51fd\u6570\u6216\u8005\u8868\u8fbe\u5f0f\u6765\u51b3\u5b9atext\u503c<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u8ba9\u4f60\u7684text\u66f4\u53ef\u63a7\uff0c\u90a3\u9009\u62e9\u662f\u521b\u5efa\u4e00\u4e2a\u4f9d\u8d56\u76d1\u63a7\u5c5e\u6027\uff08dependent observable\uff09\uff0c\u7136\u540e\u5728\u5b83\u7684\u6267\u884c\u51fd\u6570\u91cc\u7f16\u7801\uff0c\u51b3\u5b9a\u5e94\u8be5\u663e\u793a\u4ec0\u4e48\u6837\u7684text\u6587\u672c\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code>The item is &lt;span data-bind=\"text: priceRating\"&gt;&lt;\/span&gt; today.\n\n&lt;script type=\"text\/javascript\"&gt;\n    var viewModel = {\n        price: ko.observable(24.95)\n    };\n\n    viewModel.priceRating = ko.dependentObservable(function () {\n        returnthis.price() &gt;50?\"expensive\" : \"affordable\";\n    }, viewModel);\n&lt;\/script&gt;<\/code><\/pre>\n<p>\u73b0\u5728\uff0ctext\u6587\u672c\u5c06\u5728\u201cexpensive\u201d\u548c\u201caffordable\u201d\u4e4b\u95f4\u66ff\u6362\uff0c\u53d6\u51b3\u4e8e\u4ef7\u683c\u600e\u4e48\u6539\u53d8\u3002<\/p>\n<p>\u7136\u800c\uff0c\u5982\u679c\u6709\u7c7b\u4f3c\u9700\u6c42\u7684\u8bdd\u5176\u5b9e\u6ca1\u6709\u5fc5\u8981\u518d\u58f0\u660e\u4e00\u4e2a\u4f9d\u8d56\u76d1\u63a7\u5c5e\u6027\uff08dependent observable\uff09\uff0c \u4f60\u53ea\u9700\u8981\u6309\u7167\u5982\u4e0b\u4ee3\u7801\u5199JavaScript\u8868\u8fbe\u5f0f\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n<pre><code>The item is &lt;span data-bind=\"text: price() &gt; 50 ? 'expensive' : 'affordable'\"&gt;&lt;\/span&gt; today.<\/code><\/pre>\n<p>\u7ed3\u679c\u662f\u4e00\u6837\u7684\uff0c\u4f46\u6211\u4eec\u4e0d\u9700\u8981\u518d\u58f0\u660e\u4f9d\u8d56\u76d1\u63a7\u5c5e\u6027\uff08dependent observable\uff09\u3002<\/p>\n<p><strong>\u6ce82\uff1a\u5173\u4e8eHTML encoding<\/strong><\/p>\n<p>\u56e0\u4e3a\u8be5\u7ed1\u5b9a\u662f\u8bbe\u7f6e\u5143\u7d20\u7684innerText\u6216textContent \uff08\u800c\u4e0d\u662finnerHTML\uff09\uff0c\u6240\u4ee5\u5b83\u662f\u5b89\u5168\u7684\uff0c\u6ca1\u6709HTML\u6216\u8005\u811a\u672c\u6ce8\u5165\u7684\u98ce\u9669\u3002\u4f8b\u5982\uff1a\u5982\u679c\u4f60\u7f16\u5199\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>viewModel.myMessage(\"&lt;i&gt;Hello, world!&lt;\/i&gt;\");<\/code><\/pre>\n<p>\u2026 \u5b83\u4e0d\u4f1a\u663e\u793a\u659c\u4f53\u5b57\uff0c\u800c\u662f\u539f\u6837\u8f93\u51fa\u6807\u7b7e\u3002\u5982\u679c\u4f60\u9700\u8981\u663e\u793aHTML\u5185\u5bb9\uff0c\u8bf7\u53c2\u8003html\u7ed1\u5b9a.<\/p>\n<p><strong>\u6ce83\uff1a\u5173\u4e8eIE 6\u7684\u767d\u7a7a\u683cwhitespace<\/strong><\/p>\n<p>IE6\u6709\u4e2a\u5947\u602a\u7684\u95ee\u9898\uff0c\u5982\u679c span\u91cc\u6709\u7a7a\u683c\u7684\u8bdd\uff0c\u5b83\u5c06\u81ea\u52a8\u53d8\u6210\u4e00\u4e2a\u7a7a\u7684span\u3002\u5982\u679c\u4f60\u60f3\u7f16\u5199\u5982\u4e0b\u7684\u4ee3\u7801\u7684\u8bdd\uff0c\u90a3Knockout\u5c06\u4e0d\u8d77\u4efb\u4f55\u4f5c\u7528\uff1a<\/p>\n<pre><code>Welcome, &lt;span data-bind=\"text: userName\"&gt;&lt;\/span&gt; to our web site.<\/code><\/pre>\n<p>\u2026 IE6 \u5c06\u4e0d\u4f1a\u663e\u793aspan\u4e2d\u95f4\u7684\u90a3\u4e2a\u7a7a\u683c\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u8fd9\u6837\u7684\u4ee3\u7801\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<pre><code>Welcome, &lt;span data-bind=\"text: userName\"&gt;&amp;nbsp;&lt;\/span&gt; to our web site.<\/code><\/pre>\n<p>IE6\u4ee5\u540e\u7248\u672c\u548c\u5176\u5b83\u6d4f\u89c8\u5668\u90fd\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898<\/p>\n<p><strong>\u4f9d\u8d56\u6027<\/strong><\/p>\n<p>\u9664KO\u6838\u5fc3\u7c7b\u5e93\u5916\uff0c\u65e0\u4f9d\u8d56\u3002<\/p>\n<h2>3&nbsp;&nbsp; html \u7ed1\u5b9a<\/h2>\n<p><strong>\u76ee\u7684<\/strong><\/p>\n<p>html\u7ed1\u5b9a\u5230DOM\u5143\u7d20\u4e0a\uff0c\u4f7f\u5f97\u8be5\u5143\u7d20\u663e\u793a\u7684HTML\u503c\u4e3a\u4f60\u7ed1\u5b9a\u7684\u53c2\u6570\u3002\u5982\u679c\u5728\u4f60\u7684view model\u91cc\u58f0\u660eHTML\u6807\u8bb0\u5e76\u4e14render\u7684\u8bdd\uff0c\u90a3\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><\/p>\n<pre><code>&lt;div data-bind=\"html: details\"&gt;&lt;\/div&gt; \n\n&lt;script type=\"text\/javascript\"&gt;\n    var viewModel = {\n        details: ko.observable() \/\/ Initially blank\n    };\n\n    viewModel.details(\"&lt;em&gt;For further details, view the report &lt;a href='report.html'&gt;here&lt;\/a&gt;.&lt;\/em&gt;\");\n    \/\/ HTML content appears\n&lt;\/script&gt;  <\/code><\/pre>\n<p><strong>\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>\u4e3b\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp; KO\u8bbe\u7f6e\u8be5\u53c2\u6570\u503c\u5230\u5143\u7d20\u7684innerHTML\u5c5e\u6027\u4e0a\uff0c\u5143\u7d20\u4e4b\u524d\u7684\u5185\u5bb9\u5c06\u88ab\u8986\u76d6\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u5982\u679c\u53c2\u6570\u662f\u76d1\u63a7\u5c5e\u6027observable\u7684\uff0c\u90a3\u5143\u7d20\u7684\u5185\u5bb9\u5c06\u6839\u636e\u53c2\u6570\u503c\u7684\u53d8\u5316\u800c\u66f4\u65b0\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u90a3\u5143\u7d20\u7684\u5185\u5bb9\u5c06\u53ea\u8bbe\u7f6e\u4e00\u6b21\u5e76\u4e14\u4ee5\u540e\u4e0d\u5728\u66f4\u65b0\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u5982\u679c\u4f60\u4f20\u7684\u662f\u4e0d\u662f\u6570\u5b57\u6216\u8005\u5b57\u7b26\u4e32\uff08\u4f8b\u5982\u4e00\u4e2a\u5bf9\u8c61\u6216\u8005\u6570\u7ec4\uff09\uff0c\u90a3\u663e\u793a\u7684\u6587\u672c\u5c06\u662fyourParameter.toString()\u7684\u7b49\u4ef7\u5185\u5bb9\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>\u5176\u5b83\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u65e0<\/p>\n<p><strong>\u6ce8\uff1a\u5173\u4e8eHTML encoding<\/strong><\/p>\n<p>\u56e0\u4e3a\u8be5\u7ed1\u5b9a\u8bbe\u7f6e\u5143\u7d20\u7684innerHTML\uff0c\u4f60\u5e94\u8be5\u6ce8\u610f\u4e0d\u8981\u4f7f\u7528\u4e0d\u5b89\u5168\u7684HTML\u4ee3\u7801\uff0c\u56e0\u4e3a\u6709\u53ef\u80fd\u5f15\u8d77\u811a\u672c\u6ce8\u5165\u653b\u51fb\u3002\u5982\u679c\u4f60\u4e0d\u786e\u4fe1\u662f\u5426\u5b89\u5168\uff08\u6bd4\u5982\u663e\u793a\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\uff09\uff0c\u90a3\u4f60\u5e94\u8be5\u4f7f\u7528text\u7ed1\u5b9a\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u7ed1\u5b9a\u53ea\u662f\u8bbe\u7f6e\u5143\u7d20\u7684text \u503cinnerText\u548ctextContent\u3002<\/p>\n<p><strong>\u4f9d\u8d56\u6027<\/strong><\/p>\n<p>\u9664KO\u6838\u5fc3\u7c7b\u5e93\u5916\uff0c\u65e0\u4f9d\u8d56\u3002<\/p>\n<h2>4&nbsp;&nbsp; css \u7ed1\u5b9a<\/h2>\n<p><strong>\u76ee\u7684<\/strong><\/p>\n<p>css\u7ed1\u5b9a\u662f\u6dfb\u52a0\u6216\u5220\u9664\u4e00\u4e2a\u6216\u591a\u4e2aCSS class\u5230DOM\u5143\u7d20\u4e0a\u3002 \u975e\u5e38\u6709\u7528\uff0c\u6bd4\u5982\u5f53\u6570\u5b57\u53d8\u6210\u8d1f\u6570\u65f6\u9ad8\u4eae\u663e\u793a\u3002\uff08\u6ce8\uff1a\u5982\u679c\u4f60\u4e0d\u60f3\u5e94\u7528CSS class\u800c\u662f\u60f3\u5f15\u7528style\u5c5e\u6027\u7684\u8bdd\uff0c\u8bf7\u53c2\u8003<strong>style\u7ed1\u5b9a<\/strong>\u3002\uff09<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><\/p>\n<pre><code>&lt;div data-bind=\"css: { profitWarning: currentProfit() &lt; 0 }\"&gt;\n   Profit Information\n&lt;\/div&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n    var viewModel = {\n        currentProfit: ko.observable(150000)\n        \/\/ Positive value, so initially we don't apply the \"profitWarning\" class\n    };\n\n    viewModel.currentProfit(-50);\n    \/\/ Causes the \"profitWarning\" class to be applied\n&lt;\/script&gt;<\/code><\/pre>\n<p>\u6548\u679c\u5c31\u662f\u5f53currentProfit \u5c0f\u4e8e0\u7684\u65f6\u5019\uff0c\u6dfb\u52a0profitWarning CSS class\u5230\u5143\u7d20\u4e0a\uff0c\u5982\u679c\u5927\u4e8e0\u5219\u5220\u9664\u8fd9\u4e2aCSS class\u3002<\/p>\n<p><strong>\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>\u4e3b\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp; \u8be5\u53c2\u6570\u662f\u4e00\u4e2aJavaScript\u5bf9\u8c61\uff0c\u5c5e\u6027\u662f\u4f60\u7684CSS class\u540d\u79f0\uff0c\u503c\u662f\u6bd4\u8f83\u7528\u7684true\u6216false\uff0c\u7528\u6765\u51b3\u5b9a\u662f\u5426\u5e94\u8be5\u4f7f\u7528\u8fd9\u4e2aCSS class\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u4f60\u53ef\u4ee5\u4e00\u6b21\u8bbe\u7f6e\u591a\u4e2aCSS class\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684view model\u6709\u4e00\u4e2a\u53ebisServre\u7684\u5c5e\u6027\uff0c<\/p>\n<pre><code>&lt;div data-bind=\"css: { profitWarning: currentProfit() &lt; 0, majorHighlight: isSevere }\"&gt;<\/code><\/pre>\n<p>&nbsp;&nbsp;&nbsp; \u975e\u5e03\u5c14\u503c\u4f1a\u88ab\u89e3\u6790\u6210\u5e03\u5c14\u503c\u3002\u4f8b\u5982\uff0c 0\u548cnull\u88ab\u89e3\u6790\u6210false\uff0c21\u548c\u975enull\u5bf9\u8c61\u88ab\u89e3\u6790\u6210true\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u5982\u679c\u53c2\u6570\u662f\u76d1\u63a7\u5c5e\u6027observable\u7684\uff0c\u90a3\u968f\u7740\u503c\u7684\u53d8\u5316\u5c06\u4f1a\u81ea\u52a8\u6dfb\u52a0\u6216\u8005\u5220\u9664\u8be5\u5143\u7d20\u4e0a\u7684CSS class\u3002\u5982\u679c\u4e0d\u662f\uff0c\u90a3CSS class\u5c06\u4f1a\u53ea\u6dfb\u52a0\u6216\u8005\u5220\u9664\u4e00\u6b21\u5e76\u4e14\u4ee5\u540e\u4e0d\u5728\u66f4\u65b0\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55JavaScript\u8868\u8fbe\u5f0f\u6216\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u3002KO\u5c06\u7528\u5b83\u7684\u6267\u884c\u7ed3\u679c\u6765\u51b3\u5b9a\u662f\u5426\u5e94\u7528\u6216\u5220\u9664CSS class\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>\u5176\u5b83\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u65e0<\/p>\n<p><strong>\u6ce8\uff1a\u5e94\u7528\u7684CSS class\u7684\u540d\u5b57\u4e0d\u662f\u5408\u6cd5\u7684JavaScript\u53d8\u91cf\u547d\u540d<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u4f7f\u7528my-class class\uff0c\u4f60\u4e0d\u80fd\u5199\u6210\u8fd9\u6837\uff1a<\/p>\n<pre><code>&lt;div data-bind=\"css: { my-class: someValue }\"&gt;...&lt;\/div&gt;<\/code><\/pre>\n<p>\u2026 \u56e0\u4e3amy-class\u4e0d\u662f\u4e00\u4e2a\u5408\u6cd5\u7684\u547d\u540d\u3002\u89e3\u51b3\u65b9\u6848\u662f\uff1a\u5728my-class\u4e24\u8fb9\u52a0\u5f15\u53f7\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f7f\u7528\u3002\u8fd9\u662f\u4e00\u4e2a\u5408\u6cd5\u7684JavaScript \u5bf9\u8c61 \u6587\u5b57\uff08\u4eceJSON\u6280\u672f\u89c4\u683c\u8bf4\u660e\u6765\u8bf4\uff0c\u4f60\u4efb\u4f55\u65f6\u5019\u90fd\u5e94\u8be5\u8fd9\u6837\u4f7f\u7528\uff0c\u867d\u7136\u4e0d\u662f\u5fc5\u987b\u7684\uff09\u3002\u4f8b\u5982,<\/p>\n<pre><code>&lt;div data-bind=\"css: { 'my-class': someValue }\"&gt;...&lt;\/div&gt;<\/code><\/pre>\n<p><strong>\u4f9d\u8d56\u6027<\/strong><\/p>\n<p>\u9664KO\u6838\u5fc3\u7c7b\u5e93\u5916\uff0c\u65e0\u4f9d\u8d56\u3002<\/p>\n<h2>5&nbsp;&nbsp; style \u7ed1\u5b9a<\/h2>\n<p><strong>\u76ee\u7684<\/strong><\/p>\n<p>style\u7ed1\u5b9a\u662f\u6dfb\u52a0\u6216\u5220\u9664\u4e00\u4e2a\u6216\u591a\u4e2aDOM\u5143\u7d20\u4e0a\u7684style\u503c\u3002\u6bd4\u5982\u5f53\u6570\u5b57\u53d8\u6210\u8d1f\u6570\u65f6\u9ad8\u4eae\u663e\u793a\uff0c\u6216\u8005\u6839\u636e\u6570\u5b57\u663e\u793a\u5bf9\u5e94\u5bbd\u5ea6\u7684Bar\u3002\uff08\u6ce8\uff1a\u5982\u679c\u4f60\u4e0d\u662f\u5e94\u7528style\u503c\u800c\u662f\u5e94\u7528CSS class\u7684\u8bdd\uff0c\u8bf7\u53c2\u8003<strong>CSS\u7ed1\u5b9a<\/strong>\u3002\uff09<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><\/p>\n<pre><code>&lt;div data-bind=\"style: { color: currentProfit() &lt; 0 ? 'red' : 'black' }\"&gt;\n   Profit Information\n&lt;\/div&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n    var viewModel = {\n        currentProfit: ko.observable(150000) \/\/ Positive value, so initially black\n    };\n    viewModel.currentProfit(-50); \/\/ Causes the DIV's contents to go red\n&lt;\/script&gt;<\/code><\/pre>\n<p>\u5f53currentProfit \u5c0f\u4e8e0\u7684\u65f6\u5019div\u7684style.color\u662f\u7ea2\u8272\uff0c\u5927\u4e8e\u7684\u8bdd\u662f\u9ed1\u8272\u3002<\/p>\n<p><strong>\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>\u4e3b\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp; \u8be5\u53c2\u6570\u662f\u4e00\u4e2aJavaScript\u5bf9\u8c61\uff0c\u5c5e\u6027\u662f\u4f60\u7684style\u7684\u540d\u79f0\uff0c\u503c\u662f\u8be5style\u9700\u8981\u5e94\u7528\u7684\u503c\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u4f60\u53ef\u4ee5\u4e00\u6b21\u8bbe\u7f6e\u591a\u4e2astyle\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684view model\u6709\u4e00\u4e2a\u53ebisServre\u7684\u5c5e\u6027\uff0c<\/p>\n<pre><code>&lt;div data-bind=\"style: { color: currentProfit() &lt; 0 ? 'red' : 'black', fontWeight: isSevere() ? 'bold' : '' }\"&gt;...&lt;\/div&gt;<\/code><\/pre>\n<p>&nbsp;&nbsp;&nbsp; \u5982\u679c\u53c2\u6570\u662f\u76d1\u63a7\u5c5e\u6027observable\u7684\uff0c\u90a3\u968f\u7740\u503c\u7684\u53d8\u5316\u5c06\u4f1a\u81ea\u52a8\u6dfb\u52a0\u6216\u8005\u5220\u9664\u8be5\u5143\u7d20\u4e0a\u7684style\u503c\u3002\u5982\u679c\u4e0d\u662f\uff0c\u90a3style\u503c\u5c06\u4f1a\u53ea\u5e94\u7528\u4e00\u6b21\u5e76\u4e14\u4ee5\u540e\u4e0d\u5728\u66f4\u65b0\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55JavaScript\u8868\u8fbe\u5f0f\u6216\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u3002KO\u5c06\u7528\u5b83\u7684\u6267\u884c\u7ed3\u679c\u6765\u51b3\u5b9a\u662f\u5426\u5e94\u7528\u6216\u5220\u9664style\u503c\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>\u5176\u5b83\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u65e0<\/p>\n<p><strong>\u6ce8\uff1a\u5e94\u7528\u7684style\u7684\u540d\u5b57\u4e0d\u662f\u5408\u6cd5\u7684JavaScript\u53d8\u91cf\u547d\u540d<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u9700\u8981\u5e94\u7528font-weight\u6216\u8005text-decoration\uff0c\u4f60\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528\uff0c\u800c\u662f\u8981\u4f7f\u7528style\u5bf9\u5e94\u7684JavaScript\u540d\u79f0\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u9519\u8bef\uff1a&nbsp;{ font-weight: someValue }; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u6b63\u786e\uff1a&nbsp;{ fontWeight: someValue }<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u9519\u8bef\uff1a&nbsp;{ text-decoration: someValue };&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u6b63\u786e\uff1a&nbsp;{ textDecoration: someValue }<\/p>\n<p>\u53c2\u8003\uff1astyle\u540d\u79f0\u548c\u5bf9\u5e94\u7684JavaScript \u540d\u79f0\u5217\u8868\u3002<\/p>\n<p><strong>\u4f9d\u8d56\u6027<\/strong><\/p>\n<p>\u9664KO\u6838\u5fc3\u7c7b\u5e93\u5916\uff0c\u65e0\u4f9d\u8d56\u3002<\/p>\n<h2>6&nbsp;&nbsp; attr \u7ed1\u5b9a<\/h2>\n<p><strong>\u76ee\u7684<\/strong><\/p>\n<p>attr \u7ed1\u5b9a\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8bbe\u7f6eDOM\u5143\u7d20\u7684\u4efb\u4f55\u5c5e\u6027\u503c\u3002\u4f60\u53ef\u4ee5\u8bbe\u7f6eimg\u7684src\u5c5e\u6027\uff0c\u8fde\u63a5\u7684href\u5c5e\u6027\u3002\u4f7f\u7528\u7ed1\u5b9a\uff0c\u5f53\u6a21\u578b\u5c5e\u6027\u6539\u53d8\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u81ea\u52a8\u66f4\u65b0\u3002<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><\/p>\n<pre><code>&lt;a data-bind=\"attr: { href: url, title: details }\"&gt;\n    Report\n&lt;\/a&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n    var viewModel = {\n        url: ko.observable(\"year-end.html\"),\n        details: ko.observable(\"Report including final year-end statistics\")\n    };\n&lt;\/script&gt;<\/code><\/pre>\n<p>\u5448\u73b0\u7ed3\u679c\u662f\u8be5\u8fde\u63a5\u7684href\u5c5e\u6027\u88ab\u8bbe\u7f6e\u4e3ayear-end.html\uff0c title\u5c5e\u6027\u88ab\u8bbe\u7f6e\u4e3aReport including final year-end statistics\u3002<\/p>\n<p><strong>\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>\u4e3b\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp; \u8be5\u53c2\u6570\u662f\u4e00\u4e2aJavaScript\u5bf9\u8c61\uff0c\u5c5e\u6027\u662f\u4f60\u7684attribute\u540d\u79f0\uff0c\u503c\u662f\u8be5attribute\u9700\u8981\u5e94\u7528\u7684\u503c\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u5982\u679c\u53c2\u6570\u662f\u76d1\u63a7\u5c5e\u6027observable\u7684\uff0c\u90a3\u968f\u7740\u503c\u7684\u53d8\u5316\u5c06\u4f1a\u81ea\u52a8\u6dfb\u52a0\u6216\u8005\u5220\u9664\u8be5\u5143\u7d20\u4e0a\u7684attribute\u503c\u3002\u5982\u679c\u4e0d\u662f\uff0c\u90a3attribute\u503c\u5c06\u4f1a\u53ea\u5e94\u7528\u4e00\u6b21\u5e76\u4e14\u4ee5\u540e\u4e0d\u5728\u66f4\u65b0\u3002<\/p>\n<p>&nbsp; &nbsp;&nbsp;<strong>\u5176\u5b83\u53c2\u6570<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\u65e0<\/p>\n<p><strong>\u6ce8\uff1a\u5e94\u7528\u7684\u5c5e\u6027\u540d\u5b57\u4e0d\u662f\u5408\u6cd5\u7684JavaScript\u53d8\u91cf\u547d\u540d<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u8981\u7528\u7684\u5c5e\u6027\u540d\u79f0\u662fdata-something\u7684\u8bdd\uff0c\u4f60\u4e0d\u80fd\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code>&lt;div data-bind=\"attr: { data-something: someValue }\"&gt;...&lt;\/div&gt;<\/code><\/pre>\n<p>\u2026 \u56e0\u4e3adata-something \u4e0d\u662f\u4e00\u4e2a\u5408\u6cd5\u7684\u547d\u540d\u3002\u89e3\u51b3\u65b9\u6848\u662f\uff1a\u5728data-something\u4e24\u8fb9\u52a0\u5f15\u53f7\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f7f\u7528\u3002\u8fd9\u662f\u4e00\u4e2a\u5408\u6cd5\u7684JavaScript \u5bf9\u8c61 \u6587\u5b57\uff08\u4eceJSON\u6280\u672f\u89c4\u683c\u8bf4\u660e\u6765\u8bf4\uff0c\u4f60\u4efb\u4f55\u65f6\u5019\u90fd\u5e94\u8be5\u8fd9\u6837\u4f7f\u7528\uff0c\u867d\u7136\u4e0d\u662f\u5fc5\u987b\u7684\uff09\u3002\u4f8b\u5982,<\/p>\n<pre><code>&lt;div data-bind=\"attr: { \u2018data-something\u2019: someValue }\"&gt;...&lt;\/div&gt;<\/code><\/pre>\n<p><strong>\u4f9d\u8d56\u6027<\/strong><\/p>\n<p>\u9664KO\u6838\u5fc3\u7c7b\u5e93\u5916\uff0c\u65e0\u4f9d\u8d56\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1&nbsp;&nbsp; visible \u7ed1\u5b9a \u76ee\u7684 visible\u7ed1\u5b9a\u5230DOM\u5143\u7d20\u4e0a\uff0c\u4f7f\u5f97\u8be5\u5143\u7d20\u7684hidd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182398],"tags":[],"class_list":["post-191166","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/191166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=191166"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/191166\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=191166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=191166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=191166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}