{"id":1344,"date":"2021-09-05T20:13:12","date_gmt":"2021-09-05T12:13:12","guid":{"rendered":"https:\/\/blog.odliken.top\/?p=1344"},"modified":"2023-08-12T17:23:40","modified_gmt":"2023-08-12T09:23:40","slug":"jquery%e4%b8%8eajax%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/blog.odjbinail.cn\/?p=1344","title":{"rendered":"jQuery\u4e0eAjax\u5165\u95e8"},"content":{"rendered":"<h1>jQuery \u4ecb\u7ecd\u4e0e\u4e0b\u8f7d\u5b89\u88c5<\/h1>\n<ul>\n<li>jQuery \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7 JS \u5e93\uff0c\u4f7f\u7528\u5341\u5206\u7b80\u5355<\/li>\n<li>jQuery \u7684\u6838\u5fc3\u662f<strong><em>\u9009\u62e9\u5668<\/em><\/strong>\uff0c\u7528\u4e8e\u83b7\u53d6\u9875\u9762\u5143\u7d20<\/li>\n<li>jQuery \u63d0\u4f9b\u4e86\u5927\u91cf\u9ad8\u6548\u65b9\u6cd5\uff0c\u5f00\u53d1\u901f\u5ea6\u5927\u5e45\u63d0\u5347<\/li>\n<li>jquery.com<\/li>\n<\/ul>\n<h2>jQuery \u9009\u62e9\u5668\u5b9e\u9a8c\u5ba4<\/h2>\n<pre class=\"prettyprint linenums\" ><code>&lt;!DOCTYPE html &gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta charset=&quot;UTF-8&quot;&gt;\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=utf-8&quot; \/&gt;\n&lt;title&gt;jQuery \u5b9e\u9a8c\u5ba4&lt;\/title&gt;\n&lt;style&gt;\n.myclass {\n    font-style: italic;\n    color: darkblue;\n}\n\/* \u9ad8\u4eae css \u7c7b *\/\n.highlight {\n    color: red;\n    font-size: 30px;\n    background: lightblue;\n}\n&lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div class=&quot;section&quot;&gt;\n        &lt;h2&gt;jQuery \u9009\u62e9\u5668\u5b9e\u9a8c\u5ba4&lt;\/h2&gt;\n        &lt;input style=&quot;height: 24px&quot; id=&quot;txtSelector&quot; \/&gt;\n        &lt;button id=&quot;btnSelect&quot; style=&quot;height: 30px&quot;&gt;\u9009\u62e9&lt;\/button&gt;\n        &lt;hr \/&gt;\n        &lt;div&gt;\n            &lt;p id=&quot;welcome&quot;&gt;\u6b22\u8fce\u6765\u5230\u9009\u62e9\u5668\u5b9e\u9a8c\u5ba4&lt;\/p&gt;\n            &lt;ul&gt;\n                &lt;li&gt;\u641c\u7d22\u5f15\u64ce\uff1a&lt;a href=&quot;http:\/\/www.baidu.com&quot;&gt;\u767e\u5ea6&lt;\/a&gt; &lt;span&gt; &lt;a\n                        style=&quot;color: darkgreen&quot; href=&quot;http:\/\/www.so.com&quot;&gt;360&lt;\/a&gt;\n                &lt;\/span&gt;\n                &lt;\/li&gt;\n                &lt;li&gt;\u7535\u5b50\u90ae\u7bb1\uff1a&lt;a href=&quot;http:\/\/mail.163.com&quot;&gt;\u7f51\u6613\u90ae\u7bb1&lt;\/a&gt; &lt;span&gt; &lt;a\n                        style=&quot;color: darkgreen&quot; href=&quot;http:\/\/mail.qq.com&quot;&gt;QQ \u90ae\u7bb1&lt;\/a&gt;\n                &lt;\/span&gt;\n                &lt;\/li&gt;\n                &lt;li&gt;\u4e2d\u56fd\u540d\u6821\uff1a&lt;a href=&quot;http:\/\/www.tsinghua.edu.cn&quot;&gt;\u6e05\u534e\u5927\u5b66&lt;\/a&gt; &lt;span&gt;\n                        &lt;a style=&quot;color: darkgreen&quot; href=&quot;https:\/\/www.pku.edu.cn\/&quot;&gt;\u5317\u4eac\u5927\u5b66&lt;\/a&gt;\n                &lt;\/span&gt;\n                &lt;\/li&gt;\n            &lt;\/ul&gt;\n            &lt;span class=&quot;myclass &quot;&gt;\u6211\u662f\u62e5\u6709 myclass \u7c7b\u7684 span \u6807\u7b7e&lt;\/span&gt;\n            &lt;p class=&quot;myclass&quot;&gt;\u6211\u662f\u62e5\u6709 myclass \u7684 p \u6807\u7b7e&lt;\/p&gt;\n            &lt;form id=&quot;info&quot; action=&quot;#&quot; method=&quot;get&quot;&gt;\n                &lt;div&gt;\n                    \u7528\u6237\u540d\uff1a&lt;input type=&quot;text&quot; name=&quot;uname&quot; value=&quot;admin&quot; \/&gt; \u5bc6\u7801\uff1a&lt;input\n                        type=&quot;password&quot; name=&quot;upsd&quot; value=&quot;123456&quot; \/&gt;\n                &lt;\/div&gt;\n                &lt;div&gt;\n                    \u5a5a\u59fb\u72b6\u51b5\uff1a &lt;select id=&quot;marital_status&quot;&gt;\n                        &lt;option value=&quot;1&quot;&gt;\u672a\u5a5a&lt;\/option&gt;\n                        &lt;option value=&quot;2&quot;&gt;\u5df2\u5a5a&lt;\/option&gt;\n                        &lt;option value=&quot;3&quot;&gt;\u79bb\u5f02&lt;\/option&gt;\n                        &lt;option value=&quot;4&quot;&gt;\u4e27\u5076&lt;\/option&gt;\n                    &lt;\/select&gt;\n                &lt;\/div&gt;\n                &lt;div class=&quot;left clear-left&quot;&gt;\n                    &lt;input type=&quot;submit&quot; value=&quot;\u63d0\u4ea4&quot; \/&gt; &lt;input type=&quot;reset&quot; value=&quot;\u91cd\u7f6e&quot; \/&gt;\n                &lt;\/div&gt;\n            &lt;\/form&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n    &lt;script type=&quot;text\/javascript&quot; src=&quot;js\/jquery-3.6.0.min.js&quot;&gt;&lt;\/script&gt;\n    &lt;script type=&quot;text\/javascript&quot;&gt;     document.getElementById(&quot;btnSelect&quot;).onclick=function(){\n            var selector=document.getElementById(&quot;txtSelector&quot;).value;\n            \/\/jquery \u7684\u9009\u62e9\u5668\u65b9\u6cd5\n            \/\/a \u9009\u62e9\u5668\u8868\u8fbe\u5f0f\n            $(&quot;*&quot;).removeClass(&quot;highlight&quot;);\n            $(selector).addClass(&quot;highlight&quot;);\n        }\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p><img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828124144289.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828124144289.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u57fa\u672c\u9009\u62e9\u5668<\/h2>\n<p><img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828125206684.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828125206684.png\" alt=\"\" \/><\/p><\/noscript>\n<ul>\n<li>\n<p>id \u9009\u62e9\u5668\u4f7f\u7528&quot;#id \u503c&quot;\u8fdb\u884c\u9009\u62e9<br \/>\ncss \u9009\u62e9\u5668\u4f7f\u7528&quot;.css \u7c7b\u540d&quot;\u8fdb\u884c\u9009\u62e9<br \/>\n*\/      <\/p>\n<\/li>\n<li>\n<p>\/\/\u5c06\u9009\u62e9\u9ad8\u4eae<br \/>\n$(&quot;#marital_status&quot;).addClass(&quot;highlight&quot;);<br \/>\n\/\/\u4e0e\u8fd9\u7b49\u4ef7 document.getElementById(&quot;btnSelect&quot;)<\/p>\n<\/li>\n<\/ul>\n<h2>\u5c42\u53e0\u9009\u62e9\u5668<\/h2>\n<ul>\n<li>\n<p>\u5c42\u53e0\u9009\u62e9\u5668\u662f\u6839\u636e\u5143\u7d20\u7684\u4f4d\u7f6e\u5173\u7cfb\u6765\u83b7\u53d6\u5143\u7d20\u7684\u9009\u62e9\u5668\u8868\u8fbe\u5f0f<br \/>\n<img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828133337799.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828133337799.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u5c5e\u6027\u9009\u62e9\u5668<\/h2>\n<\/li>\n<li>\n<p>\u5c5e\u6027\u9009\u62e9\u5668\u662f\u6839\u636e\u5143\u7d20\u7684\u5c5e\u6027\u503c\u6765\u9009\u62e9\u5143\u7d20\u7684\u9009\u62e9\u5668\u8868\u8fbe\u5f0f<br \/>\n<img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828134312131.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828134312131.png\" alt=\"\" \/><\/p><\/noscript>\n<\/li>\n<li>\n<p>a[href='<a href=\"http:\/\/www.baidu.com\">http:\/\/www.baidu.com<\/a>']  \u9009\u62e9\u767e\u5ea6<\/p>\n<\/li>\n<li>\n<p>a[href^='<a href=\"http:\/\/mail&#039;]\u4ee5 mail \u5f00\u5934\">http:\/\/mail&#039;%5D \u4ee5 mail \u5f00\u5934<\/a><\/p>\n<\/li>\n<li>\n<p>a[href*='edu.cn'] \u5305\u542b edu.cn \u7684<\/p>\n<\/li>\n<\/ul>\n<h2>\u4f4d\u7f6e\u9009\u62e9\u5668<\/h2>\n<ul>\n<li>\n<p>\u4f4d\u7f6e\u9009\u62e9\u5668\u662f\u6307\u901a\u8fc7\u4f4d\u7f6e\u83b7\u53d6\u6307\u5b9a\u7684\u5143\u7d20\uff0c\u4f8b\u5982\u201c\u83b7\u53d6\u7b2c 3 \u4e2a\u5143\u7d20\u201d      0123<br \/>\n<img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828151402774.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828151402774.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u8868\u5355\u9009\u62e9\u5668<\/h2>\n<\/li>\n<li>\n<p>\u8868\u5355\u9009\u62e9\u5668\u662f\u83b7\u53d6\u8868\u5355\u5143\u7d20\u7684\u7b80\u5316\u5f62\u5f0f\uff0c\u4f8b\u5982:\u83b7\u53d6\u6240\u6709\u6587\u672c\u6846<br \/>\n<img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828151743605.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828151743605.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u64cd\u4f5c\u5143\u7d20\u5c5e\u6027<\/h2>\n<pre class=\"prettyprint linenums\" ><code>sample1.html\n&lt;script type=&quot;text\/javascript&quot; src=&quot;js\/jquery-3.6.0.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script type=&quot;text\/javascript&quot;&gt;\n    var href_attr=  $(&quot;a[href*=&#039;163&#039;]&quot;).attr(&quot;href&quot;);\n    alert(href_attr);\n    $(&quot;a[href*=&#039;163&#039;]&quot;).attr(&quot;href&quot;,&quot;http:\/\/www.163.com&quot;);\n    var attr=$(&quot;a&quot;).attr(&quot;href&quot;);\/\/\u8fd4\u56de\u7b2c\u4e00\u4e2a href\n    alert(attr);\/\/attr \u7684\u6392\u9664\u7684\u4f7f\u7528\u529e\u6cd5\n    $(&quot;a&quot;).removeAttr(&quot;href&quot;);\n&lt;\/script&gt;<\/code><\/pre>\n<h2>\u64cd\u4f5c\u5143\u7d20\u7684 CSS \u6837\u5f0f<\/h2>\n<\/li>\n<li>\n<p>css()\u2013\u83b7\u53d6\u6216\u8bbe\u7f6e\u5339\u914d\u5143\u7d20\u7684\u6837\u5f0f\u5c5e\u6027<\/p>\n<\/li>\n<li>\n<p>addClass()-\u4e3a\u6bcf\u4e2a\u5339\u914d\u7684\u5143\u7d20\u6dfb\u52a0\u6307\u5b9a\u7684\u7c7b\u540d<\/p>\n<\/li>\n<li>\n<p>removeClass()-\u4ece\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\u4e2d\u5220\u9664\u5168\u90e8\u6216\u8005\u6307\u5b9a\u7684\u7c7b<\/p>\n<pre class=\"prettyprint linenums\" ><code>&lt;script type=&quot;text\/javascript&quot; src=&quot;js\/jquery-3.6.0.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script type=&quot;text\/javascript&quot;&gt;\n    $(&quot;a&quot;).css(&quot;color&quot;,&quot;red&quot;);\n    $(&quot;a&quot;).css({&quot;color&quot;:&quot;red&quot;,&quot;font-weight&quot;:&quot;bold&quot;,&quot;font-style&quot;:&quot;italic&quot;});\n    $(&quot;li&quot;).addClass(&quot;highlight myclass&quot;);\n    $(&quot;p&quot;).removeClass(&quot;myclass&quot;);\/\/\u79fb\u9664\u6307\u5b9a\u5143\u7d20\u7684 css \u6307\u5b9a\u7c7b\n    var color=$(&quot;a&quot;).css(&quot;color&quot;);\/\/\u83b7\u53d6\u6307\u5b9a\u7684 css \u6307\u5b9a\u503c\n    alert(color);\n&lt;\/script&gt;<\/code><\/pre>\n<h2>\u8bbe\u7f6e\u5143\u7d20\u5185\u5bb9<\/h2>\n<\/li>\n<li>\n<p>val()- \u83b7\u53d6\u6216\u8bbe\u7f6e\u8f93\u5165\u9879\u7684\u503c<\/p>\n<\/li>\n<li>\n<p>text()\u2013\u83b7\u53d6\u6216\u8bbe\u7f6e\u5143\u7d20\u7684\u7eaf\u6587\u672c<\/p>\n<\/li>\n<li>\n<p>html()-\u83b7\u53d6\u6216\u8bbe\u7f6e\u5143\u7d20\u5185\u90e8\u7684 HTML<\/p>\n<pre class=\"prettyprint linenums\" ><code>&lt;script type=&quot;text\/javascript&quot; src=&quot;js\/jquery-3.6.0.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script type=&quot;text\/javascript&quot;&gt;\n    $(&quot;input[name=&#039;uname&#039;]&quot;).val(&quot;administrator&quot;);\/\/\u8bbe\u7f6e\u6587\u672c\u6846\u7684\u6570\u636e\n    var v=$(&quot;input[name=&#039;uname&#039;]&quot;).val();\/\/\u83b7\u53d6\u524d\u9762\u9009\u62e9\u5668\u9009\u4e2d\u5143\u7d20\u7684\u5185\u5bb9\n    alert(v);\/\/var \u4f5c\u7528\u5728\u8f93\u5165\u9879\u4e0a\uff0c\u7528\u4e8e\u83b7\u53d6\u548c\u8bbe\u7f6e\u8868\u5355\u8f93\u5165\u9879\u7684\u503c\n    \/*\n        text \u4e0e html \u65b9\u6cd5\u6700\u5927\u7684\u533a\u522b\u5728\u4e8e\u5bf9\u4e8e\u6587\u672c\u4e2d\u7684 html \u6807\u7b7e\u662f\u5426\u8fdb\u884c\u8f6c\u4e49\n        \u5728\u5e73\u65f6\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u5982\u679c\u6ca1\u6709\u7279\u522b\u5fc5\u8981\uff0c\u4f18\u5148\u4f7f\u7528 text \u65b9\u6cd5\u8bbe\u7f6e\u6216\u83b7\u53d6\u6807\u7b7e\u7684\u5185\u5bb9\uff0c\n        \u9664\u975e\u662f\u5728\u7eaf\u6587\u672c\u4e2d\u5305\u542b\u4e86\u6807\u7b7e\uff0c\u800c\u6211\u4eec\u7684\u4efb\u52a1\u5fc5\u987b\u52a0\u8fd9\u4e2a\u6807\u7b7e\u8fdb\u884c\u52a8\u6001\u7684\u89e3\u91ca\u6267\u884c\uff0c\u6b64\u65f6\u624d\u4f1a\u4f7f\u7528 html\n        *\/\n    \/\/&lt;span class=&quot;myclass &quot;&gt;\u6211\u662f\u62e5\u6709 myclass \u7c7b\u7684 span \u6807\u7b7e&lt;\/span&gt; \u5c06\u4e2d\u95f4\u7684\u5185\u5bb9\u201c\u6211\u662f\u62e5\u6709\u3002\u3002\u6807\u7b7e\u201d\u6539\u53d8\n    \/\/$(&quot;span.myclass&quot;).text(&quot;&lt;b&gt;\u9504\u79be\u65e5\u5f53\u5348,\u6c57\u6ef4\u79be\u4e0b\u571f&lt;\/b&gt;&quot;);\/\/&lt;b&gt;\u4f1a\u663e\u793a\u51fa\u6765\n    $(&quot;span.myclass&quot;).html(&quot;&lt;b&gt;\u9504\u79be\u65e5\u5f53\u5348,\u6c57\u6ef4\u79be\u4e0b\u571f&lt;\/b&gt;&quot;);\/\/\u52a0\u7c97\n    \/\/\u83b7\u53d6\n    var vspan=$(&quot;span.myclass&quot;).html();\n    alert(vspan);\/\/&lt;b&gt;\u9504\u79be\u65e5\u5f53\u5348,\u6c57\u6ef4\u79be\u4e0b\u571f&lt;\/b&gt;\n&lt;\/script&gt;<\/code><\/pre>\n<h2>jQuery \u4e8b\u4ef6\u5904\u7406\u65b9\u6cd5<\/h2>\n<\/li>\n<li>\n<p>on(&quot; click&quot; , function)-\u4e3a\u9009\u4e2d\u7684\u9875\u9762\u5143\u7d20\u7ed1\u5b9a\u5355\u51fb\u4e8b\u4ef6<\/p>\n<\/li>\n<li>\n<p>click(functiQn)-\u662f\u7ed1\u5b9a\u4e8b\u4ef6\u7684\u7b80\u5199\u5f62\u5f0f<\/p>\n<\/li>\n<li>\n<p>\u5904\u7406\u65b9\u6cd5\u4e2d\u63d0\u4f9b\u4e86 event \u53c2\u6570\u5305\u542b\u4e86\u4e8b\u4ef6\u7684\u76f8\u5173\u4fe1\u606f<\/p>\n<\/li>\n<\/ul>\n<p><img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828201131163.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210828201131163.png\" alt=\"\" \/><\/p><\/noscript>\n<ul>\n<li>\n<p>\u5355\u51fb\uff0c\u80cc\u666f\u53d8\u4e3a\u9ec4\u8272\uff0c\u7528\u6237\u540d\u8f93\u5165\u6709\u7a7a\u683c\uff0c\u80cc\u666f\u53d8\u4e3a\u7ea2\u8272<\/p>\n<pre class=\"prettyprint linenums\" ><code>&lt;script type=&quot;text\/javascript&quot; src=&quot;js\/jquery-3.6.0.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script type=&quot;text\/javascript&quot;&gt;\n    $(&quot;p.myclass&quot;).on(&quot;click&quot;,function(){\n        \/\/$(this)\u662f\u6307\u5f53\u524d\u4e8b\u4ef6\u4ea7\u751f\u7684\u5bf9\u8c61 myclass \u7684 p \u6807\u7b7e\n        $(this).css(&quot;background-color&quot;,&quot;yellow&quot;);\n    });\n    \/\/onclick \u7684\u7b80\u5199\u5f62\u5f0f,\u53ef\u8bfb\u6027\u548c\u4f7f\u7528\u66f4\u7b80\u5355\n    $(&quot;span.myclass&quot;).click(function(){\n        $(this).css(&quot;background-color&quot;,&quot;lightpink&quot;);\n    });\n    \/\/\u7ed1\u5b9a\u952e\u76d8\u7684\u5355\u51fb\u4e8b\u4ef6\/\u5c5e\u6027\u9009\u62e9\u5668\u3001\u952e\u76d8\u7684\u5b8c\u6574\u7684\u6309\u4e0b\u5f39\u8d77\u64cd\u4f5c\n    $(&quot;input[name=&#039;uname&#039;]&quot;).keypress(function(event){\n        console.log(event);\n        if(event.keyCode==32){\n            $(this).css(&quot;color&quot;,&quot;red&quot;);\n        }\n    });\n&lt;\/script&gt;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5982\u679c\u60f3\u8ba9\u6587\u672c\u6846\u5931\u53bb\u7126\u70b9\u65f6\u8fdb\u884c\u9a8c\u8bc1\uff0c\u9700\u8981\u89e6\u53d1\u54ea\u4e2a jQuery \u4e8b\u4ef6---------blur<\/p>\n<\/li>\n<li>\n<p>\u5f53\u9009\u62e9\u4e0b\u62c9\u5217\u8868\u7684\u67d0\u4e00\u9879\u65f6\uff0c\u53ef\u4ee5\u89e6\u53d1\u54ea\u4e2a jQuery \u4e8b\u4ef6\u2014\u2014change<\/p>\n<\/li>\n<\/ul>\n<h1>Ajax \u4ecb\u7ecd<\/h1>\n<ul>\n<li>Asynchronous JavaScript And XML(\u5f02\u6b65\u7684 JavaScript \u548c XML)<\/li>\n<li>Ajax \u53ef\u4ee5\u5728\u4e0d\u5237\u65b0\u9875\u9762\u7684\u524d\u63d0\u4e0b\uff0c\u8fdb\u884c\u9875\u9762\u5c40\u90e8\u66f4\u65b0<\/li>\n<li>Ajax \u4e0d\u662f\u65b0\u7684\u6280\u672f\uff0cAjax \u5e76\u4e0d\u662f W3C \u7684\u6807\u51c6<\/li>\n<\/ul>\n<h2>\u521b\u5efa XMLHttpRequest \u5bf9\u8c61<\/h2>\n<ul>\n<li>\n<p>Ajax \u7684\u4f7f\u7528\u6d41\u7a0b<\/p>\n<ul>\n<li>\n<p>\u521b\u5efa XmlHttpRequest \u5bf9\u8c61<\/p>\n<\/li>\n<li>\n<p>\u53d1\u9001 Ajax \u8bf7\u6c42<\/p>\n<\/li>\n<li>\n<p>\u5904\u7406\u670d\u52a1\u5668\u54cd\u5e94<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>XMLHttpRequest \u7528\u4e8e\u5728\u540e\u53f0\u4e0e\u670d\u52a1\u5668\u4ea4\u6362\u6570\u636e\uff0c\u662f AJAX \u7684\u6838\u5fc3<\/p>\n<\/li>\n<li>\n<p>XMLHttpRequest \u5e76\u4e0d\u662f W3C \u7684\u6807\u51c6\uff0c\u4e0d\u540c\u6d4f\u89c8\u5668\u7684\u521b\u5efa\u65b9\u5f0f\u4e0d\u540c<br \/>\n<img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210830164211997.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210830164211997.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u53d1\u9001 Ajax \u8bf7\u6c42\u53ca\u5904\u7406\u54cd\u5e94<\/h2>\n<\/li>\n<li>\n<p>xmlhttp.open()\u7528\u4e8e\u521b\u5efa\u8bf7\u6c42<\/p>\n<\/li>\n<li>\n<p>xmlhttp.send()\u7528\u4e8e\u53d1\u9001\u8bf7\u6c42<\/p>\n<\/li>\n<li>\n<p>xmlhttp.onreadystatechange()\u4e8b\u4ef6\u7528\u4e8e\u76d1\u542c AJAX \u7684\u6267\u884c\u8fc7\u7a0b<\/p>\n<\/li>\n<li>\n<p>xmlhttp.readyState \u5c5e\u6027\u8bf4\u660e XMLHttpRequest \u5f53\u524d\u72b6\u6001<\/p>\n<\/li>\n<li>\n<p>xmlhttp.status \u5c5e\u6027\u670d\u52a1\u5668\u54cd\u5e94\u72b6\u6001\u7801\uff0c200:\u6210\u529f  404:\u672a\u627e\u5230...<br \/>\n<img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210830165402482.png\"  alt=\"\" \/><br \/>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210830165402482.png\" alt=\"\" \/><br \/><\/noscript>\n<img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210830165956227.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210830165956227.png\" alt=\"\" \/><\/p><\/noscript>\n<pre class=\"prettyprint linenums\" ><code>\/**\n* Servlet implementation class ContentServlet\n*\/\n@WebServlet(&quot;\/content&quot;)\npublic class ContentServlet extends HttpServlet {\nprivate static final long serialVersionUID = 1L;\n\/**\n * @see HttpServlet#HttpServlet()\n *\/\npublic ContentServlet() {\n    super();\n    \/\/ TODO Auto-generated constructor stub\n}\n\/**\n * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)\n *\/\nprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n\/\/\u5bf9\u4e8e ajax \u901a\u8fc7\u524d\u540e\u7aef\u901a\u8baf\u7684\u65f6\u5019\uff0c\u5982\u679c\u524d\u9762\u662f ajax \u8fdb\u884c\u8bf7\u6c42\u53d1\u9001\u7684\u8bdd\uff0cservlet \u4e2d\u4e0d\u518d\u8fdb\u884c\u9875\u9762\u7684\u8df3\u8f6c\uff0c\u800c\u662f\u76f4\u63a5\u8fd4\u56de\u6211\u4eec\u8981\u4ea7\u751f\u7684\u4fe1\u606f\uff0c\u6216\u8005\u63d0\u4f9b\u7528\u6237\u7684\u6570\u636e\u5c31\u53ef\u4ee5\u4e86\uff0c\u901a\u5e38\u8fd9\u79cd\u6570\u636e\u7684\u683c\u5f0f\uff0c\u6211\u4eec\u4f1a\u91c7\u7528 json \u7684\u5f62\u5f0f\u56de\u4f20\u3002\n    response.getWriter().println(&quot;&lt;b style=&#039;color:red&#039;&gt;I am sever content&lt;\/b&gt;&quot;);\n}\n}<\/code><\/pre>\n<pre class=\"prettyprint linenums\" ><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta charset=&quot;UTF-8&quot;&gt;\n&lt;title&gt;Insert title here&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;input id=&quot;btnload&quot; type=&quot;button&quot; value=&quot;\u52a0\u8f7d&quot;&gt;\n&lt;div id=&quot;divContent&quot;&gt; &lt;\/div&gt;\n&lt;script&gt;      document.getElementById(&quot;btnload&quot;).onclick=function(){\n        \/\/\u521b\u5efa XmlHttpRequest \u5bf9\u8c61\n        var xmlhttp;\n        if(window.XMLHttpRequest){\n            xmlhttp=new XMLHttpRequest();\n        }else{\n            xmlhttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);\n        }\n        console.log(xmlhttp);\n        \/\/\u53d1\u9001 Ajax \u8bf7\u6c42\n        xmlhttp.open(&quot;GET&quot;,&quot;\/ajax\/content&quot;,true);\n        xmlhttp.send();\n        \/\/\u5904\u7406\u670d\u52a1\u5668\u54cd\u5e94\n        xmlhttp.onreadystatechange=function(){\n            \/\/\u54cd\u5e94\u5df2\u7ecf\u88ab\u63a5\u6536\u5230\u3001\u670d\u52a1\u5668\u5904\u7406\u6210\u529f\n            if(xmlhttp.readyState==4&amp;&amp;xmlhttp.status==200){\n                var t=xmlhttp.responseText;\n                alert(t);\n                \/\/\u5c06\u5185\u5bb9\u663e\u793a\u51fa\u6765\n                document.getElementById(&quot;divContent&quot;).innerHTML=t;\n            }\n        }\n    }\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u5229\u7528 Ajax \u5b9e\u73b0\u65b0\u95fb\u5217\u8868<\/h2>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>public class News {\n    private String title;\n    private String date;\n    private String source;\n    private String content;\n    public News() {\n        super();\n    }\n    public News(String title, String date, String source, String content) {\n        super();\n        this.title = title;\n        this.date = date;\n        this.source = source;\n        this.content = content;\n    }\n    public String getTitle() {\n        return title;\n    }\n    public void setTitle(String title) {\n        this.title = title;\n    }\n    public String getDate() {\n        return date;\n    }\n    public void setDate(String date) {\n        this.date = date;\n    }\n    public String getSource() {\n        return source;\n    }\n    public void setSource(String source) {\n        this.source = source;\n    }\n    public String getContent() {\n        return content;\n    }\n    public void setContent(String content) {\n        this.content = content;\n    }\n}<\/code><\/pre>\n<pre class=\"prettyprint linenums\" ><code>\/**\n * Servlet implementation class NewsListServlet\n *\/\n@WebServlet(&quot;\/news_list&quot;)\npublic class NewsListServlet extends HttpServlet {\n    private static final long serialVersionUID = 1L;\n    \/**\n     * @see HttpServlet#HttpServlet()\n     *\/\n    public NewsListServlet() {\n        super();\n        \/\/ TODO Auto-generated constructor stub\n    }\n    \/**\n     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse\n     *      response)\n     *\/\n    protected void doGet(HttpServletRequest request, HttpServletResponse response)\n            throws ServletException, IOException {\n        List list = new ArrayList();\n        list.add(new News(&quot;TUB:2024424 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;tube&quot;, &quot;...&quot;));\n        list.add(new News(&quot;TUB:2024425 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;tube&quot;, &quot;...&quot;));\n        list.add(new News(&quot;TUB:2024426 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;tube&quot;, &quot;...&quot;));\n        list.add(new News(&quot;TUB:2024427 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;tube&quot;, &quot;...&quot;));\n        String json= JSON.toJSONString(list);\n        System.out.println(json);\n        response.setContentType(&quot;text\/html;charset=UTF-8&quot;);\n        response.getWriter().println(json);\n    }\n}<\/code><\/pre>\n<pre class=\"prettyprint linenums\" ><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta charset=&quot;UTF-8&quot;&gt;\n&lt;title&gt;Insert title here&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div id=&quot;container&quot;&gt;&lt;\/div&gt;\n    &lt;script type=&quot;text\/javascript&quot;&gt;\n        \/\/\u521b\u5efa XmlHttpRequest \u5bf9\u8c61\n        var xmlhttp;\n        if (window.XMLHttpRequest) {\n            xmlhttp = new XMLHttpRequest();\n        } else {\n            xmlhttp = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);\n        }\n        \/\/\u53d1\u9001 Ajax \u8bf7\u6c42\n        xmlhttp.open(&quot;GET&quot;, &quot;\/ajax\/news_list&quot;, true);\n        xmlhttp.send();\n        \/\/\u5904\u7406\u670d\u52a1\u5668\u54cd\u5e94\n        xmlhttp.onreadystatechange = function() {\n            if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) {\n                var text = xmlhttp.responseText;\n                console.log(text);\n                var json = JSON.parse(text);\n                console.log(json);\n                var html=&quot;&quot;;\n                for (var i = 0; i &lt; json.length; i++) {\n                    var news = json[i];\n                    html = html + &quot;&lt;h1&gt;&quot; + news.title + &quot;&lt;\/h1&gt;&quot;;\n                    html = html + &quot;&lt;h2&gt;&quot; + news.date + &quot;&nbsp;&quot; + news.source\n                            + &quot;&lt;\/h2&gt;&quot;;\n                    html = html + &quot;&lt;hr\/&gt;&quot;\n                }\n                document.getElementById(&quot;container&quot;).innerHTML = html;\n            }\n        }\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u540c\u6b65\u4e0e\u5f02\u6b65\u7684\u533a\u522b<\/h2>\n<ul>\n<li>\u540c\u6b65\uff1a\u76f8\u5f53\u4e8e\u4ee3\u7801\u8fdb\u884c\u4e86\u4e00\u4e2a\u7b49\u5f85\u7684\u72b6\u6001\uff0c\u6570\u636e\u4e0d\u8fd4\u56de\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u5411\u4e0b\u6267\u884c<\/li>\n<li>\u5f02\u6b65\uff1aajax \u662f\u4e0d\u4f1a\u5904\u4e8e\u4ee3\u7801\u7684\u963b\u585e\u72b6\u6001\u7684\uff0c\u7a0b\u5e8f\u4f9d\u7136\u4f1a\u5411\u4e0b\u6267\u884c\uff0c\u4f46\u662f\u6570\u636e\u8fd4\u56de\u7684\u65f6\u5019\u662f\u89e6\u53d1 xmlhttp.onreadystatechange = function() {}\u6765\u8fdb\u884c\u83b7\u53d6\u5904\u7406\u7684\u3002<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>\u540c\u6b65\n\/\/\u53d1\u9001 Ajax \u8bf7\u6c42\n        \/\/true \u4ee3\u8868\u5f02\u6b65\u6267\u884c  false \u4ee3\u8868\u540c\u6b65\u6267\u884c\n        xmlhttp.open(&quot;GET&quot;, &quot;\/ajax\/news_list&quot;, false);\n        xmlhttp.send();\n        console.log(&quot;\u8bf7\u6c42\u53d1\u9001\u5b8c\u6210&quot;);\n        \/*\u540c\u6b65\u5904\u7406\u4ee3\u7801\n        if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) {\n            var text = xmlhttp.responseText;\n            console.log(text);\n            var json = JSON.parse(text);\n            console.log(json);\n            var html=&quot;&quot;;\n            for (var i = 0; i &lt; json.length; i++) {\n                var news = json[i];\n                html = html + &quot;&lt;h1&gt;&quot; + news.title + &quot;&lt;\/h1&gt;&quot;;\n                html = html + &quot;&lt;h2&gt;&quot; + news.date + &quot;&nbsp;&quot; + news.source\n                        + &quot;&lt;\/h2&gt;&quot;;\n                html = html + &quot;&lt;hr\/&gt;&quot;\n            }\n            document.getElementById(&quot;container&quot;).innerHTML = html;\n        }\n        *\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/                                            NewsListServlet.java:\nprotected void doGet(HttpServletRequest request,HttpServletResponse response)\n            throws ServletException, IOException {\n        try {\n            Thread.sleep(5000);\n        } catch (InterruptedException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        }<\/code><\/pre>\n<h2>jQuery \u5bf9 Ajax \u7684\u652f\u6301<\/h2>\n<ul>\n<li>jQuery \u5bf9 AJAX \u8fdb\u884c\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86$.ajax()\u65b9\u6cd5<\/li>\n<li>\u8bed\u6cd5: $.ajax(options)<\/li>\n<li>\u4f7f\u7528 jQuery \u5bf9 Ajax \u8fdb\u884c\u652f\u6301\uff0c\u5728\u8fdb\u884c\u9875\u9762\u5904\u7406\u65f6\uff0cServlet \u7ed9 Ajax \u8fd4\u56de\u6570\u636e\u7684\u6570\u636e\u683c\u5f0f\u4e3a json\uff0c\u9700\u8981\u5728\uff08dataType\uff09\u5c5e\u6027\u4e2d\u8868\u73b0\u51fa\u6765\u3002<br \/>\n<img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210831204007036.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/09\/image-20210831204007036.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>Ajax \u51fd\u6570\u7684\u4f7f\u7528<\/h2>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>package com.imooc.ajax;\nimport java.io.IOException;\nimport java.util.ArrayList;\nimport java.util.List;\nimport javax.servlet.ServletException;\nimport javax.servlet.annotation.WebServlet;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\nimport com.alibaba.fastjson.JSON;\n\/**\n * Servlet implementation class NewsListServlet\n *\/\n@WebServlet(&quot;\/news_list&quot;)\npublic class NewsListServlet extends HttpServlet {\n    private static final long serialVersionUID = 1L;\n    \/**\n     * @see HttpServlet#HttpServlet()\n     *\/\n    public NewsListServlet() {\n        super();\n        \/\/ TODO Auto-generated constructor stub\n    }\n    \/**\n     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse\n     *      response)\n     *\/\n    protected void doGet(HttpServletRequest request, HttpServletResponse response)\n            throws ServletException, IOException {\n        String type=request.getParameter(&quot;t&quot;);\n        List list = new ArrayList();\n        if(type!=null&amp;&amp;type.equals(&quot;pypl&quot;)) {\n            list.add(new News(&quot;PYPL:2024424 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;PYPL&quot;, &quot;...&quot;));\n            list.add(new News(&quot;PYPL:2024425 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;PYPL&quot;, &quot;...&quot;));\n            list.add(new News(&quot;PYPL:2024426 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;PYPL&quot;, &quot;...&quot;));\n            list.add(new News(&quot;PYPL:2024427 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;PYPL&quot;, &quot;...&quot;));\n        }else if(type==null||type.equals(&quot;tiobe&quot;)) {\n            list.add(new News(&quot;TUB:2024424 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;tube&quot;, &quot;...&quot;));\n            list.add(new News(&quot;TUB:2024425 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;tube&quot;, &quot;...&quot;));\n            list.add(new News(&quot;TUB:2024426 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;tube&quot;, &quot;...&quot;));\n            list.add(new News(&quot;TUB:2024427 \u4e2a\u4eba\u80a1\u4efd\u4e8c\u4e2a\u4eba\u4e2a\u4eba&quot;, &quot;2018-5-1&quot;, &quot;tube&quot;, &quot;...&quot;));\n        }\n        String json= JSON.toJSONString(list);\n        System.out.println(json);\n        response.setContentType(&quot;text\/html;charset=UTF-8&quot;);\n        response.getWriter().println(json);\n    }\n}<\/code><\/pre>\n<pre class=\"prettyprint linenums\" ><code>jquery_news.html\uff1a\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta charset=&quot;UTF-8&quot;&gt;\n&lt;title&gt;Insert title here&lt;\/title&gt;\n&lt;script type=&quot;text\/javascript&quot; src=&quot;js\/jquery-3.6.0.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script type=&quot;text\/javascript&quot;&gt;\n    $(function(){\n        $.ajax({\n            &quot;url&quot;:&quot;\/ajax\/news_list&quot;,\n            &quot;type&quot;:&quot;get&quot;,\n            &quot;data&quot;:&quot;t=tiobe&quot;,\/\/{&quot;t&quot;:&quot;pypl&quot;,&quot;abc&quot;:&quot;123,&quot;uu&quot;:&quot;777&quot;}\n            &quot;dataType&quot;:&quot;json&quot;,\n            &quot;success&quot;:function(json){\n                console.log(json);\n                for(var i=0;i&lt;json.length;i++){\n                    $(&quot;#container&quot;).append(&quot;&lt;h1&gt;&quot;+json[i].title+&quot;&lt;\/h1&gt;&quot;);\n                }\n            },\n            &quot;error&quot;:function(xmlhttp,errorText){\n                console.log(xmlhttp);\n                console.log(errorText);\n                if(xmlhttp.status==&quot;405&quot;){\n                    alert(&quot;\u65e0\u6548\u7684\u8bf7\u6c42\u65b9\u5f0f&quot;);\n                }else if(xmlhttp.status==&quot;404&quot;){\n                    alert(&quot;\u672a\u627e\u5230 URL \u8d44\u6e90&quot;);\n                }else if(xmlhttp.status==&quot;500&quot;){\n                    alert(&quot;\u670d\u52a1\u5668\u5185\u90e8\u9519\u8bef\uff0c\u8bf7\u8054\u7cfb\u7ba1\u7406\u5458&quot;);\n                }else{\n                    alert(&quot;\u4ea7\u751f\u5f02\u5e38\uff0c\u8bf7\u8054\u7cfb\u7ba1\u7406\u5458&quot;);\n                }\n            }\n        })\n    })\n&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div id=&quot;container&quot;&gt;&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u5b9e\u73b0\u4e8c\u7ea7\u8054\u52a8\u83dc\u5355<\/h2>\n<pre class=\"prettyprint linenums\" ><code>package com.imooc.ajax;\npublic class Channel {\n    private String code;\n    private String name;\n    public Channel() {\n        super();\n    }\n    public Channel(String code, String name) {\n        super();\n        this.code = code;\n        this.name = name;\n    }\n    public String getCode() {\n        return code;\n    }\n    public void setCode(String code) {\n        this.code = code;\n    }\n    public String getName() {\n        return name;\n    }\n    public void setName(String name) {\n        this.name = name;\n    }\n}<\/code><\/pre>\n<pre class=\"prettyprint linenums\" ><code>package com.imooc.ajax;\nimport java.io.IOException;\nimport java.util.ArrayList;\nimport java.util.List;\nimport javax.servlet.ServletException;\nimport javax.servlet.annotation.WebServlet;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\nimport com.alibaba.fastjson.JSON;\n\/**\n * Servlet implementation class ChannelServlet\n *\/\n@WebServlet(&quot;\/channel&quot;)\npublic class ChannelServlet extends HttpServlet {\n    private static final long serialVersionUID = 1L;\n    \/**\n     * @see HttpServlet#HttpServlet()\n     *\/\n    public ChannelServlet() {\n        super();\n        \/\/ TODO Auto-generated constructor stub\n    }\n    \/**\n     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)\n     *\/\n    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n        String level=request.getParameter(&quot;level&quot;);\n        String parent=request.getParameter(&quot;parent&quot;);\n        List chlist=new ArrayList();\n        if(level.equals(&quot;1&quot;)){\n            chlist.add(new Channel(&quot;ai&quot;,&quot;\u524d\u6cbf\/\u533a\u5757\u94fe\/\u4eba\u5de5\u667a\u80fd&quot;));\n            chlist.add(new Channel(&quot;web&quot;,&quot;\u524d\u7aef\/\u5c0f\u7a0b\u5e8f\/JS&quot;));\n        }else if(level.equals(&quot;2&quot;)){\n            if(parent.equals(&quot;ai&quot;)) {\n                chlist.add(new Channel(&quot;micro&quot;,&quot;\u5fae\u670d\u52a1&quot;));\n                chlist.add(new Channel(&quot;micro&quot;,&quot;\u5fae\u670d\u52a1&quot;));\n                chlist.add(new Channel(&quot;micro&quot;,&quot;\u5fae\u670d\u52a1&quot;));\n            }else if(parent.equals(&quot;web&quot;)) {\n                chlist.add(new Channel(&quot;html&quot;,&quot;HTML&quot;));\n                chlist.add(new Channel(&quot;css&quot;,&quot;CSS&quot;));\n                chlist.add(new Channel(&quot;other&quot;,&quot;...&quot;));\n            }\n        }\n        String json= JSON.toJSONString(chlist);\n        response.setContentType(&quot;text\/html;charset=UTF-8&quot;);\n        response.getWriter().println(json);\n    }\n}<\/code><\/pre>\n<pre class=\"prettyprint linenums\" ><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta charset=&quot;UTF-8&quot;&gt;\n&lt;title&gt;Insert title here&lt;\/title&gt;\n&lt;script type=&quot;text\/javascript&quot; src=&quot;js\/jquery-3.6.0.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script type=&quot;text\/javascript&quot;&gt;\n    $(function(){\n        $.ajax({\n            &quot;url&quot; : &quot;\/ajax\/channel&quot;,\n            &quot;data&quot; : {&quot;level&quot; : &quot;1&quot;},\n            &quot;type&quot; : &quot;get&quot; ,\n            &quot;dataType&quot; : &quot;json&quot; ,\n            &quot;success&quot; : function(json){\n                console.log(json);\n                for(var i = 0 ; i &lt; json.length ; i++){\n                    var ch = json[i];\n                    $(&quot;#lv1&quot;).append(&quot;&lt;option value=&#039;&quot; + ch.code + &quot;&#039;&gt;&quot; + ch.name + &quot;&lt;\/option&gt;&quot;)\n                }\n            }\n        })\n    })\n    $(function(){\n        $(&quot;#lv1&quot;).on(&quot;change&quot; , function(){\n            var parent = $(this).val();\n            console.log(parent);\n            $.ajax({\n                &quot;url&quot; : &quot;\/ajax\/channel&quot; ,\n                &quot;data&quot; : {&quot;level&quot; : &quot;2&quot; , &quot;parent&quot; : parent},\n                &quot;dataType&quot; : &quot;json&quot; ,\n                &quot;type&quot; : &quot;get&quot; ,\n                &quot;success&quot; : function(json){\n                    console.log(json);\n                    \/\/\u79fb\u9664\u6240\u6709 lv2 \u4e0b\u7684\u539f\u59cb option \u9009\u9879\n                    $(&quot;#lv2&gt;option&quot;).remove();\n                    for(var i = 0 ; i &lt; json.length ; i++){\n                        var ch = json[i];\n                        $(&quot;#lv2&quot;).append(&quot;&lt;option value=&#039;&quot; + ch.code +&quot;&#039;&gt;&quot; + ch.name + &quot;&lt;\/option&gt;&quot;)\n                    }\n                }\n            })\n        })\n    })\n&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;select id=&quot;lv1&quot; style=&quot;width:200px;height:30px&quot;&gt;\n    &lt;option selected=&quot;selected&quot;&gt;\u8bf7\u9009\u62e9&lt;\/option&gt;\n&lt;\/select&gt;\n&lt;select id=&quot;lv2&quot; style=&quot;width:200px;height:30px&quot;&gt;&lt;\/select&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\nhttp:\/\/localhost:8080\/ajax\/cascade_menu.html<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>jQuery \u4ecb\u7ecd\u4e0e\u4e0b\u8f7d\u5b89\u88c5 jQuery \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7 JS \u5e93\uff0c\u4f7f\u7528\u5341\u5206\u7b80\u5355 jQuery \u7684\u6838\u5fc3\u662f\u9009\u62e9\u5668 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-1344","post","type-post","status-publish","format-standard","hentry","category-web"],"_links":{"self":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts\/1344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1344"}],"version-history":[{"count":1,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts\/1344\/revisions"}],"predecessor-version":[{"id":4339,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts\/1344\/revisions\/4339"}],"wp:attachment":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}