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&tp=webp&wxfrom=5&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;"> <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 文件中的 <a> 标签,为其添加名为 “first_Js” 的 id 属性:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <aid="first_Js">传送门</a></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>放在 <head>,<span style="color: black;">亦</span><span style="color: black;">能够</span>放在 <body>!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">但<span style="color: black;">意见</span>放在 <body>,<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;"><body></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <div id="container"> </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <h1>我爱鱼C</h1></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><p>WWW.FihsC.com - 让编程改变世界</p></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <aid="first_Js">传送门</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> </div></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <script type="text/javascript"></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;"> </script>></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;"><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&tp=webp&wxfrom=5&wx_lazy=1&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;"> first_Js.onclick = function(e)</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;"> e.preventDefault();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> first_Js.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;">演示效果<img src="https://mmbiz.qpic.cn/mmbiz_png/b96CibCt70iaajvl7fD4ZCicMcjhXMp1v6UibM134tIsO1j5yqHyNhh9arj090oAL7zGhRJRq6cFqFOlDZMleLl4pw/640?tp=webp&wxfrom=5&wx_lazy=1&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&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p><img src="http://mmbiz.qpic.cn/mmbiz_gif/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTZgL4lVtyw9F9GCAPnkKglmZ0dJ5JQ7WicUbibKQYJ4iasiaibLyNA5bUhww/0?wx_fmt=gif&tp=webp&wxfrom=5&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&tp=webp&wxfrom=5&wx_lazy=1&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&tp=webp&wxfrom=5&wx_lazy=1&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;"><!doctype html></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><html></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><head></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <meta charset="utf-8"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><title>欢迎来到HTML世界</title></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <link href="0009CSS.css" rel="stylesheet"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"></head></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;"> <div id="container"> </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <h1>我爱鱼C</h1></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <p>WWW.FihsC.com - 让编程改变世界</p></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><aid="first_Js">传送门</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> </div></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <script src="first_Js.js"></script></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;"></html></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&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><img src="http://mmbiz.qpic.cn/mmbiz_png/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTmZibCYbK1Br6s7ziaL4pYRjgWEfuWOicbxns0JoCZSgLFAW0ER05MpcwQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&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;">已开启大幕,欢迎订阅 <img src="http://mmbiz.qpic.cn/mmbiz_png/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTO0Q3NLPLtGonW1rtYXjiaJN6sibve97YVH5fQNJ0LdkBW07oPOKpYTPA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><img src="http://mmbiz.qpic.cn/mmbiz_gif/G5ialO5KHfDxeOomB2VTtPGufgQJzRbNTr85ghyXZylhEMXEFmH7ibujMph3g14JNUXU16DAGBLq7jNuBicVqBA4g/0?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;">戳原文,更有料!
论坛是一个舞台,让我们在这里尽情的释放自己。 你的见解真是独到,让我受益匪浅。 你的话语真是温暖如春,让我心生感激。
页:
[1]