设置按钮及其样式:
function over(){
//给按钮改个背景,#fff RGB=red g=green blue
//document.getElementById("btn").style.backgroundColor = "#f00";
//$("#btn") jquery.js在原生js上进一步封装后,简化代码编写
document.getElementById("btn").className = "overClass";
}
function out(){
//document.getElementById("btn").style.backgroundColor = "#0f0";
document.getElementById("btn").className = "outClass";
}
<style type="text/css">
.overClass{
background-color:#F90;
width:400px;
height:80px;
}
.outClass{
background:#F60;/*backgrond应用更广泛*/
width:200px;
</style>
<body>
<input type="button" id="btn" value="点我登陆" onClick="login('zq','123')"
onMouseOver="over()" onMouseOut="out()"/>
<span id="msg"></span>
</body>
增加文本框:
<input type="text" id="txt" value="我是文本框" onChange="change()"/><!--onFocus="alert('获取焦点');" onBlur="alert('我失去焦点')"-->
function change(){//当文本框的内容发生改变的时候
var txt = document.getElementById("txt").value;
if(txt.length>10){
alert("用户名长度不能超过10");
}
}
当用户输入10以上长度时,点击空白区域,会弹出窗口“用户名长度不能超过10”
浏览器对象:
window对象、Location对象、History对象
alert(window.innerHeight+"---"+window.innerWidth);//显示当前窗口的长和宽,随着窗口而变化
function openNew(){
window.location.href="";//可以用此种方法直接点出来location,以免打错
window.history.back();
//window.document;//
location.href="http://www.baidu.com";
history.back();//后退
history.forward();//前进
history.go(1);//1-前进一个页面 2前进2个页面 -2 后退2个页面
//location.reload();//刷新当前页面
//window.open("http://www.baidu.com");//打开一个新的页面
//window.close();//关闭当前的页面
}
var 声明的变量,没指明类型,如何查看类型? typeof
一般数字和字符串、字符串和日期转换常用:
将数字转为字符串:
alert(typeof(String(123)));
alert(String(123+12.33));
将字符串转为数字:
var ss = "555qq5";//字符串转数字
alert(parseInt(ss));//js只有这两种,没有parseDouble
alert(parseFloat("999"));//Integer,pareseInt Float.parseFloat Double.parseDouble
将日期转换为字符串:
var d = new Date();//Date返回字符串
alert(typeof(d));
alert(String(d));//转成字符串
//d.toString();//toString也有相同效果
将字符串转换为日期:
var s = "2019-12-12 12:12:12";//字符串变成日期类型
new Date(s);
JSON:
JSON 是用于存储和传输数据的格式。通常用于服务端向网页传递数据。
{"sites":[
{"name":"Runoob","url":"www.runoob.com"},
{"name":"Google","url":"www.google.com"},
{"name":"Taobao","url":"www.taobao.com"}
]}
JSON 字符串转换为 JavaScript 对象:
var text = '{ "sites" : [' +
'{ "name":"Runoob" , "url":"www.runoob.com" },' +
'{ "name":"Google" , "url":"www.google.com" },' +
'{ "name":"Taobao" , "url":"www.taobao.com" } ]}';
var obj = JSON.parse(text);
alert(obj.sites[1].name);