JS and DOM relationship
Html browser has rendered the function code, the html source (e.g., div, p tag, etc.) to form a DOM object in memory
Document Object Model DOM (Document Object Model) standard defines the access method and process HTML documents. DOM HTML document will appear as a tree structure (a node tree) with the elements, attributes, and text.
HTML documents can be said to constitute a collection of nodes, and three common DOM node:
1. The element node: figure above <html>, <body>, <p> are all elements of the node, i.e. the label.
2. The text nodes: the content presented to the user, such as <li> the contents of JavaScript, DOM, CSS and other text.
3. attribute node: element properties, such as link attribute href <a> tag = "http://www.imooc.com".
BOM objects (the address bar of the browser, history, DOM, etc. mounted on an object)
Internal browser has JS interpreter / engine; JS html code will be executed in the engine, the result is an operation performed on the DOM object (i.e., the label is a tree within the node operation)
Add effects JS: JS operate with nothing more than just DOM objects
JS manner of introduction
JS code can be written anywhere in html, but the browser parses the code from top to bottom, note at this time whether to parse the html tag, allowing JS accessibility to the DOM object, so sometimes we put the code into html end, that is, </ html> before
Directly written in html: <script type = "text / javascript"> codes </ script>
In through the external reference: <script type = text / javascript src = ""> </ script>
To prevent slow page loads, you can put non-critical JavaScript into the bottom of the page
Declaring Variables
var variable name; variable names are case-sensitive; var statement will not pollute the global variable; the variable name with a letter, underscore, dollar sign at the beginning, later digitally
Function code segments to complete a specific function;
Common method
Output statements to html using document.write ( "")
Confire (): Message confirmation dialog; click OK to return true
Operators problem
Splicing operators: +, if the sum is a number, the connection character; As 2 + 3 + 'love' + 4 + 5 // output 5love45
PHP and different, usually with.
Logical operators or: Returns the value of true; such as:
There are a = 1;
was b = false;
var d = (a || b); // d 为 1
And logical operators: Returns the value of the variable rearmost
There are a = 22;
var b = 33;
alert (a && b); // Output 33
And operating an array of objects
JS array of key figures in increments only from 0
Note: the array brackets, JS in length; objects with braces
Use the built-in objects Js
window object
JS window object and it does not matter; the browser is an array of objects for JS to operate.
Window.open()
<script type="text/javascript"> window.open('http://www.imooc.com','_blank','width=300,height=200,menubar=no,toolbar=no, status=no,scrollbars=yes') </script>
Scope
Case 1: This parameter is not found within a function, we will always keep looking outside, until the global space (outside the function) to find the window property
Case 2: var declare variables; declared in the local variable belongs only function within the function. If the direct assignment, without var (such as: a = 10). Would have been out looking for this variable, the assignment to find it, or else assign the default window variable name
Label object look-up tables
Find a way to not h3c HTML DOM Manual
方法名如果返回是集合则getElements ,如class,P等标签都不是唯一的所以s
id唯一返回对象,P等不唯一返回集合
根据id获取
根据标签找对象
对于表单元素,可以使用name寻找
按照类名查找
根据结点查找
对象的操作
img对象 下还有多个属性
img.src img.style.width
注意 :
<div class="ch"> 中操作div对象的修改class名字属性用div.className =
CSS样式与DOM : obj.style.width
CSS属性带横线则 去之首字大写 : border-top à obj.style.borderTop
注意 :此处获取宽高是把CSS内嵌,比较方便;并且返回的是字符串形式
实战 每次点击变颜色并且边框增大10像素
Object.style.display = none/block 实现隐藏和显示
Object.className = name 实现修改类名
获取内联样式属性
获取内存中在渲染的style的值,使用obj.currrentStyle() 和 window.getComputedStyle()获取
window.getComputedStyle(obj,伪元素)[arrt] :
obj获取运算后的样式目标元素 ;
伪元素 : 一般为null,可以修改为鼠标放上去的状态‘:active’
获取的值只读即是只能获取,不能直接修改,要修改还是要通过obj.style.属性 修改
内联样式一开始不能够获取是因为一开始没有定义内联定义,但是能够初始化赋值
获取的颜色返回是RGB形式的
注意:只有IE和Opera支持使用currentStyle获取HTMLElement的计算后的样式
标准浏览器使用getComputedStyle(),对此兼容性问题,使用封装进方法中进行判断使用哪个。obj即是对象名,arrt是获取属性名
对象的创建和删除 node.html
暴力结点 nodein.html
innerHTML 是结点的一个属性值,代表结点内的内容,即是某标签内的内容
级联菜单的制作 jilianorder.html
定时器的使用
不是js的内容属于浏览器的
setTimeout (表达式,延时时间) : 设置在延迟多少时间执行一次一个表达式
clearTimeout(名); 设置清除这个延迟器
setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式;多次执行
clearInterval() 方法取消 setInterval() 的设置