美文网首页weex-js-vue
JS笔记-001-基本实现

JS笔记-001-基本实现

作者: ccccccal | 来源:发表于2018-03-22 11:38 被阅读10次

    HTML 中的脚本必须位于 <script> 与 </script> 标签之间。

    脚本可被放置在 HTML 页面的 <body> 和 <head> 部分中。

    JavaScript 能够直接写在Html输出流中,
    <script>
    document.write("<h1>This is a heading</h1>");
    document.write("<p>This is a paragraph.</p>");
    </script>
    

    您只能在 HTML 输出流中使用 document.write。 如果您在文档已加载后使用它(比如在函数中),会覆盖整个文档。

    点击事件,都需要分号括起来

    先添加type声明是一个button,onclick点击事件弹窗(alert弹窗dialog),弹窗内容使用括号加起来,标签中间声明button的内容

    <button type="button" onclick="alert('Welcome!')">点击这里</button>
    
    改变html的内容
    <!DOCTYPE html>
    <html>
    <body>
    
    <h1>我的第一段 JavaScript</h1>
    
    <p id = "demo">
    JavaScript 能改变 HTML 元素的内容。
    </p>
    
    <script>
         funcation myFuncation()//类似于封装一个方法
    {
        x= document.getElementById("demo");//找到具体元素,类似于android中ID
        x.innerHTML="Hello World !"
    }     
    </script>
    
    <button type="button" onclick="myFunction()">点击这里</button>
    </body>
    </html>
    
    
    js改变图片地址
    <!DOCTYPE html>
    <html>
    <body>
    <script>
    function changeImage()
    {
    element=document.getElementById('myimage')//获得图片控件对象
    if (element.src.match("bulbon"))//判断src是否包含"bulbon", 类似于indexOf()
      {
      element.src="/i/eg_bulboff.gif";
      }
    else
      {
      element.src="/i/eg_bulbon.gif";
      }
    }
    </script>
    
    <img id="myimage" onclick="changeImage()" src="/i/eg_bulboff.gif">
    
    <p>点击灯泡来点亮或熄灭这盏灯</p>
    
    </body>
    </html>
    
    
    JavaScript:改变 HTML 样式
    <!DOCTYPE html>
    <html>
    <body>
    
    <h1>我的第一段 JavaScript</h1>
    
    <p id="demo">
    JavaScript 能改变 HTML 元素的样式。
    </p>
    
    <script>
    function myFunction()
    {
    x=document.getElementById("demo") // 找到元素
    x.style.color="#57caa1";          // 改变样式
    }
    </script>
    
    <button type="button" onclick="myFunction()">点击这里</button>
    
    </body>
    </html>
    
    
    JavaScript:验证输入
    <!DOCTYPE html>
    <html>
    <body>
    
    <h1>我的第一段 JavaScript</h1>
    
    <p>请输入数字。如果输入值不是数字,浏览器会弹出提示框。</p>
    
    <input id="demo" type="text">
    
    <script>
    function myFunction()
    {
    var x=document.getElementById("demo").value;//通过id得到值,去做判断
    if(x==""||isNaN(x))//isNan()用于判断是否为数字,类似于number类
        {
        alert("请输入合法内容");
        }
    }
    </script>
    
    <button type="button" onclick="myFunction()">点击这里</button>
    
    </body>
    </html>
    
    

    相关文章

      网友评论

        本文标题:JS笔记-001-基本实现

        本文链接:https://www.haomeiwen.com/subject/edwoqftx.html