b1gc8v 发表于 2024-6-29 16:26:34

HTML5 | 0 0 9 - 初识JavaScript(Js)


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在前面<span style="color: black;">咱们</span>对 HTML、CSS 有了初步<span style="color: black;">认识</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>看一下 JS!</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_gif/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTdd7bKFhiayhTlia1I3qKkaRxnzv676ibckpGGLOTp3uU6YIY0Ih8WRC1Q/0?wx_fmt=gif&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>fishc<span style="color: black;"><span style="color: black;">完成<span style="color: black;">第1</span>次JS交互</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>改良下传送门,来<span style="color: black;">&nbsp;<span style="color: black;">“<span style="color: black;">解”</span></span></span>开 JS 的面纱!</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">之前<span style="color: black;">咱们</span>单击传送门,就会被传到小天才养殖场</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>,就不<span style="color: black;">转</span>了,直接按钮变成“穿越成功”</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">完成一次简单的<span style="color: black;">状态交互过程~</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">就像<a style="color: black;"><span style="color: black;"> 004 </span></a>咱们<span style="color: black;">第1</span>次<span style="color: black;">运用</span> CSS <span style="color: black;">同样</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">总<span style="color: black;">必须</span>赋予按钮一个“名字”<span style="color: black;">或</span>“标签”来让 JS 找到这个按钮。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">很简单,修改 html 文件中的 &lt;a&gt; 标签,为其添加名为 “first_Js” 的 id 属性:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;aid="first_Js"&gt;传送门&lt;/a&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">正确创建名字后,就<span style="color: black;">能够</span>创建 JS 标签来写后续的代码了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span> JS 部分放在哪里呢?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>放在 &lt;head&gt;,<span style="color: black;">亦</span><span style="color: black;">能够</span>放在 &lt;body&gt;!</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">但<span style="color: black;">意见</span>放在 &lt;body&gt;,<span style="color: black;">这般</span>避免 JS 代码提前被解析,<span style="color: black;">加强</span>加载速度。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在 body 添加代码:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;body&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;div id="container"&gt; &nbsp; &nbsp; &nbsp; &nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;h1&gt;我爱鱼C&lt;/h1&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;p&gt;WWW.FihsC.com - 让编程改变世界&lt;/p&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;aid="first_Js"&gt;传送门&lt;/a&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;script type="text/javascript"&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">var first_Js = document.getElementById("first_Js");</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/script&gt;&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;/body&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">必须</span><span style="color: black;">运用</span> var <span style="color: black;">重要</span>字,来定义一个变量对应“传送门”按钮,便于后续对按钮的设置。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span><span style="color: black;">运用</span> document 对象的 getElementBy <span style="color: black;">办法</span>,来获取 id 名所对应的元素。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你不确定,<span style="color: black;">是不是</span>对应成功,<span style="color: black;">能够</span><span style="color: black;">运用</span>:</p>alert(first_Js );<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">来进行测试,成功的话,会弹出这个提示框:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTmmFlMPNdak3Zsv8W7VPhmCd57iaNL6ChUWEv1hSv1fonn8nJjTQhtMg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>就<span style="color: black;">能够</span>,修改 first_Js的onclick(单击事件)了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有两件事,<span style="color: black;">必须</span><span style="color: black;">重视</span>:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 可<span style="color: black;">经过</span> preventDefault <span style="color: black;">办法</span>,来阻止 URL <span style="color: black;">转</span>网页。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. innerHTML 属性来修改,单击“传送门”后字体变为“穿越成功”。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码如下:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; first_Js.onclick = function(e)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first_Js.innerHTML = "穿越成功";</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">演示效果<img src="https://mmbiz.qpic.cn/mmbiz_png/b96CibCt70iaajvl7fD4ZCicMcjhXMp1v6UibM134tIsO1j5yqHyNhh9arj090oAL7zGhRJRq6cFqFOlDZMleLl4pw/640?tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_gif/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTd5kHozaKaTu8mvQ1aJ9w8jP06J1FzFEbWEeSNkDVHtdH7lKYBL0QXA/0?wx_fmt=gif&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p><img src="http://mmbiz.qpic.cn/mmbiz_gif/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTZgL4lVtyw9F9GCAPnkKglmZ0dJ5JQ7WicUbibKQYJ4iasiaibLyNA5bUhww/0?wx_fmt=gif&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="width: 50%; margin-bottom: 20px;">fishc<span style="color: black;"><span style="color: black;">面向对象</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">没错,聪明如你,<span style="color: black;">必定</span>猜到了,<span style="color: black;">必定</span><span style="color: black;">能够</span>像 <a style="color: black;"><span style="color: black;">008</span></a>CSS 样式文件<span style="color: black;">同样</span>,独立封装 JS</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">还是在目录下,创建 XXX.js,<span style="color: black;">而后</span><span style="color: black;">经过</span> xxx.html 调用<span style="color: black;">就可</span>!</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTXXIeq3bd6Qf4icpNwb54fbyTK84ECYWTjMlqDd1HITVzicINQyNicwTMg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">first_Js.js 文件,代码:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNT2T4w1X9HDg4UcGVP01wjHAu9Ykib5MnmZThy7733QOicnw4ickIx3PQ6w/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在 html 中添加<span style="color: black;">外边</span>链接,<span style="color: black;">14</span>行:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;!doctype html&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;html&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;head&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;meta charset="utf-8"&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;title&gt;欢迎来到HTML世界&lt;/title&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;link href="0009CSS.css" rel="stylesheet"&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;/head&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;body&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;div id="container"&gt; &nbsp; &nbsp; &nbsp; &nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;h1&gt;我爱鱼C&lt;/h1&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;WWW.FihsC.com - 让编程改变世界&lt;/p&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;aid="first_Js"&gt;传送门&lt;/a&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;script src="first_Js.js"&gt;&lt;/script&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;/body&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;/html&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,为了响应号召<img src="https://mmbiz.qpic.cn/mmbiz_png/b96CibCt70iaajvl7fD4ZCicMcjhXMp1v6Uq0bB37iaArQZn1mktIsS0w2xHWMKFqYRaIcPDPMbOZImcEpTFDQ1bLA/640?tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><img src="http://mmbiz.qpic.cn/mmbiz_png/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTmZibCYbK1Br6s7ziaL4pYRjgWEfuWOicbxns0JoCZSgLFAW0ER05MpcwQ/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">JavaScript庖丁解牛<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">已开启大幕,欢迎订阅&nbsp;<img src="http://mmbiz.qpic.cn/mmbiz_png/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTO0Q3NLPLtGonW1rtYXjiaJN6sibve97YVH5fQNJ0LdkBW07oPOKpYTPA/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><img src="http://mmbiz.qpic.cn/mmbiz_gif/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTr85ghyXZylhEMXEFmH7ibujMph3g14JNUXU16DAGBLq7jNuBicVqBA4g/0?wx_fmt=gif&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="width: 50%; margin-bottom: 20px;">戳原文,更有料!




4zhvml8 发表于 2024-10-3 02:07:21

论坛是一个舞台,让我们在这里尽情的释放自己。

nqkk58 发表于 2024-11-2 12:15:11

你的见解真是独到,让我受益匪浅。

j8typz 发表于 5 天前

你的话语真是温暖如春,让我心生感激。
页: [1]
查看完整版本: HTML5 | 0 0 9 - 初识JavaScript(Js)