1.json:
json是JavaScript Object Notation,是一种数据传输格式。
以下程序都是在浏览器的Console下执行的。
创建一个javaScript的对象:
var student={ id:'10001', name:'claire', loveSport:['pingpang','run','yoga'], };
之后将其序列化为json格式的数据:
var jsonStu=JSON.stringify(student);
此时输出jsonStu为:"{"id":"10001","name":"claire","loveSport":["pingpang","run","yoga"]}"
这种格式的不好看,可以加上几个参数:
JSON.stringify(student,['id','name'],' ');
输出结果为(与我们在平时测试见到的格式相同):"{
"id": "10001",
"name": "claire"
}"
还可以将Json格式数据反序列化为一个javaScript的对象。
JSON.parse();
想了解更多关于反序列化的内容,可以到菜鸟教程上学习下。网址:http://www.runoob.com/js/javascript-json-parse.html
2.方法
function add(a,b){ alert(a+b); } add(10,20);
<script> window.onload=function (){ var a=document.getElementById('10001'); alert(a); } </script>
Returns a reference to the first object with the specified value of the ID or NAME attribute.
可以看到返回的是一个对象的引用。当我们想获取到该对象的value值时,可以用a.value即可
还有其他方式可获取元素:
举例:
alert(document.getElementsByClassName('class1')[0].value); alert(document.getElementsByClassName('class1').length); alert(document.getElementsByTagName('input')[1].value); alert(document.getElementsByTagName('input').length);
还可以获取到超链接(a标签)中的文字和html:
console.log(document.getElementById('10002').innerText);
console.log(document.getElementById('10002').innerHTML);
其他事件的常用事件:
a.οnblur=function(){ alert('失去焦点') } a.onfocus=function(){ alert('聚焦') } a.onchange=function(){ alert('内容改变') } a.onclick=function(){ alert('点击事件') } a.ondblclick=function(){ alert('双击事件') } a.onmouseover=function(){ alert('鼠标上浮事件') }