欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

前端学习之(htmldom)(JSON)

程序员文章站 2022-05-30 22:57:18
找寻方法 document.getelementbyid(id) document.getelementsbytagname(name)var x = document.get...

找寻方法

document.getelementbyid(id)

document.getelementsbytagname(name)var x = document.getelementsbytagname("p");

document.getelementbyid("demo").innerhtml =?

'the first paragraph (index 0) is: ' +x[2].innerhtml;

document.getelementsbyclassname(name)

var mynodelist = document.queryselectorall("p");? //找寻所有子节点

处理方法

document.write();

document.getelementbyid(id).innerhtml="hello";

document.getelementbyid("myimage").src ="landscape.jpg";? //change attribute

document.getelementbyid("p2").style.color ="blue"; //change css

click on this text!

对节点的定义

)

parentnode childnodes[nodenumber] firstchild lastchild nextsibling previoussibling

节点属性

document.getelementbyid("id01").nodename

document.getelementbyid("id01").nodetype

document.getelementsbytagname("p").length

element_node1w3schools

attribute_node2?class = "heading" (deprecated)

text_node3w3schools

comment_node8

document_node9the html document itself (the parent of

document_type_node10

添加节点

json

javascript object notation(for stroage and exchange data)? json就是一个text,也可以理解为string

json uses javascript syntax, but the json format is text only.

text can be read and used as a data format by any programming language.

成对出现,“”括起,{}括起object []array

与xml区别:?xml has to be parsed with an xml parser. json can be parsed by a standard javascript function. (更快read write,short)

可以在object中套用object(myobj.cars["car2"]? //? ?myobj.cars.car2)如果cars是array 用myobj.cars[2]

myobj = {"name":"john","age":30, "car":null };

x = myobj.name;x = myobj["name"];

for (xin myobj) {

??? document.getelementbyid("demo").innerhtml+= x; //输出 name,age car? ? document.getelementbyid("demo").innerhtml+= myobj[x];? //输出 john 30 null}?

json.parse() to convert text into a javascript object:

var arr = ["john", "peter","sally", "jane" ];

var myjson = json.stringify(arr);? //将js转化为json

request json from the server by using an ajax request //利用xmlhttprequest