zoukankan      html  css  js  c++  java
  • javascript 学习随笔

    var carname="Volvo";
    var carname;
    

     重新声明变量后变量的值不变,在这里还是volvo

    var carname="Volvo";
    document.getElementById("demo").innerHTML=carname;
    

     改变段落元素的值

    <!DOCTYPE html>
    <html>
    <body>
    <script>
    function myFunction()
    {
    document.write("糟糕!文档消失了。");
    }
    </script>
    <h1>My First Web Page</h1>
    
    <p>My First Paragraph.</p>
    
    <button onclick="myFunction()">点击这里</button>
    
    
    
    </body>
    </html>
    

     如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖:

    var str
    

     如果一个元素只声明没有定义的话,默认值为undefined

    <!DOCTYPE html>
    <html>
    <body>
    
    <script>
    var person=new String;
    var person1;
    var car="Volvo";
    var shifei=new Boolean;
    var x= new Number; document.write(person + "<br />"); document.write(person1 + "<br />"); document.write(car + "<br />"); document.write(shifei + "<br />"); var car=null document.write(car + "<br />"); </script> </body> </html>

     声明变量类型及默认值了解

    var carname1="Bill Gates";
    var carname2='Bill Gates';
    var answer1="Nice to meet you!";
    var answer2="He is called 'Bill'";
    

     创建字符变量的时候可以使用单引号或双引号

    并且可以在引号内可以有引号 只要不与开头结尾引号对应即可

    function myFunction(a,b)
    {c=8;
    if (a>b)
      {
      return;
      }
    x=a+b
    }
    

     给未声明的变量赋值  即使是在function中   也默认c为全局变量

    greeting=(visitor=="主席")?"Dear President ":"Dear ";
    

     条件赋值法    如果visitor是主席 赋值为dear president 否则赋值为 Dear

    var day=new Date().getDay();
    switch (day)
    {
    case 0:
      x="Today it's Sunday";
      break;
    case 1:
      x="Today it's Monday";
      break;
    case 2:
      x="Today it's Tuesday";
      break;
    case 3:
      x="Today it's Wednesday";
      break;
    case 4:
      x="Today it's Thursday";
      break;
    case 5:
      x="Today it's Friday";
      break;
    case 6:
      x="Today it's Saturday";
      break;
    }
    

     使用case语句输出今天星期几

    var day=new Date().getDay();
    switch (day)
    {
    case 6:
      x="Today it's Saturday";
      break;
    case 0:
      x="Today it's Sunday";
      break;
    default:
      x="Looking forward to the Weekend";
    }
    

     默认关键词

    <!DOCTYPE html>
    <html>
    <body>
    <script>
    function changeImage()
    {
    element=document.getElementById('myimage')
    if (element.src.match("bulbon"))
      {
      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>
    

     点击更换图片

    <p id="demo">
    JavaScript 能改变 HTML 元素的内容。
    </p>
    
    <script>
    function myFunction()
    {
    x=document.getElementById("demo");  // 找到元素
    x.innerHTML="Hello JavaScript!";    // 改变内容
    }
    </script>
    
    <p>请输入数字。如果输入值不是数字,浏览器会弹出提示框。</p>
    
    <input id="demo" type="text">
    
    <script>
    function myFunction()
    {
    var x=document.getElementById("demo").value;
    if(x==""||isNaN(x))
    	{
    	alert("Not Numeric");
    	}
    }
    </script>
    
  • 相关阅读:
    toString()和toLocaleString()有什么区别
    解决浏览器跨域问题
    JavaScript排序,不只是冒泡
    input disabled的情况下在IOS设备中怎么修改颜色
    jquery api 中文文档
    javascript中var that=this的意思
    JS中(function(){xxx})(); 这种写法的意思
    http-equiv 的使用
    $(function(){......})的意思
    js中(function(){…})()立即执行函数写法理解
  • 原文地址:https://www.cnblogs.com/tianhao/p/4281957.html
Copyright © 2011-2022 走看看