Home > Web Front-end > JS Tutorial > body text

Learn about javascript dom programming together and record notes on practical experience

php是最好的语言
Release: 2018-07-27 15:41:37
Original
1353 people have browsed it

I have been learning JavaScript dom programming recently. I am afraid that I will forget it in the future, so I specially summarized and organized what I have learned. What is dom operation? Document object mode is a w3c standard.

1.JavaScript is a programming language that makes web pages interactive.

2. The method of setting the properties of browser properties is called BOM.

3. Camel case naming (myMood) is the preferred naming format for function names, method names and object attribute names.

4. When naming variables, you can use underscores to separate each word, and when naming functions, use camel case naming.

5. The function should behave like a self-sufficient script. When defining a function, we must explicitly declare all its internal variables as local variables.

6. Objects predefined by the browser are called host objects. Host objects include From, Image, document, etc.

7.DOM (document object model).

8. There are element nodes (labels) in the DOM (each element in the document is an object), text nodes (content), and attribute nodes (attributes).

9. Even if this tag has only one element in the entire document, getElementsByTagName returns an array, and the length of the array is 1.

10.getElementsByClassName returns an array of elements with the same class name.

11. When using getElemntsByClassName to specify multiple class names, you only need to separate the multiple class names with spaces
in the parameters.
12.getElementsById returns an object, which corresponds to a special element node
in the document.
13. Both getAttribute and setAttribute functions can only be used for element nodes.

14. DOM working mode: load the static content of the document first, and then refresh it dynamically. Dynamic refresh does not affect the static content of the document. Refreshing the page content does not require refreshing the page in the browser.

15. After adding an event handling function to an element, once the event occurs, the corresponding JavaScript code will be executed. The called JavaScript code can return a value, which is passed to the event handler. Assume that the event processing function is specified in the a tag onclick. When this function returns a true, the onclick event will think that the link in the a tag has been clicked. If it returns false, it will be considered that the link 2 has not been clicked. Therefore, if you want not to trigger the default behavior in the a tag, add a return false in onclick.

<li><a href="img/1.png" onclick="showpic(this);return false;">1</a> </li>
Copy after login

16. The childNodes attribute can be used to get all the child elements of any element. It is an array containing all the child elements of this element.

17. If you want to know the type of a node, you can use nodeType to view the type of the node.
nodeType=1 The node is an element node.

           =2            属性节点
           =3            文本节点
Copy after login

18.window.open() opens a New browser window.

function popUrl(winURL) {
        window.open(winURL,"popup","width:320px,height:400px");
    }
    popUrl(&#39;canvas.html&#39;);
Copy after login

19. Smooth degradation (when the browser does not support js code, it does not affect the normal function of the web page.)

20. Performance considerations
(1) Access the DOM as little as possible: Whenever you query for certain elements in the DOM, the browser searches the entire DOM tree for possible matching elements. In other words, the entire DOM tree will be traversed every time getElementBy* is used, so it is best to use it once to obtain the element and store the element in a variable.
(2) Use tags as little as possible: Too many unnecessary elements will only increase the size of the DOM tree, thereby increasing the time it takes to traverse the DOM tree to find specific elements.
(3) The best way to include scripts is to use external files and merge multiple js files together. This reduces the number of requests sent when loading a page.
(4) Place all Script tags at the end of the document, before the end of the body tag, to make the page faster.
(5) Compress script: Delete all unnecessary bytes in the script text, such as spaces and comments, to achieve the purpose of compressing the file.

21. HTTP protocol specification, the browser can download up to two files at the same time from the same name at a time.

22. If you want to use JavaScript to add some behavior to a web page, you should not let the JavaScript code have any dependence on the structure of the web page.

23. If a function has multiple exits, arrange these exits at the beginning of the function.

24. Loop to determine the js processing done after a group of a tags are clicked

function prepareGallery() {
    if (!document.getElementById) return false;
    if (!document.getElementsByTagName) return false;
    if (!document.getElementById(&#39;imagegallery&#39;)) return false;
    var gallery = document.getElementById(&#39;imagegallery&#39;);
    var links  = gallery.getElementsByTagName(&#39;a&#39;);
    for (var i = 0; i < links.length; i++){
        links[i].onclick = function () {
            showpic(this);
            return false;
        }
    }
}
Copy after login

25.addLoadEvent(): Write your own script function to add the processing that needs to be done when the page is loaded. Function

function addLoadEvent(func) {
  var onload = window.onload;
  if (typeof window.onload !=func){
  window.onload = func;
  }else {
  window.onload = function (ev) {
  oldload();
  func();
  }
  }
}
Copy after login

26.createTextNode is used to create text nodes

var txt =  document.createTextNode("hello world");
Copy after login

27.js When you want to insert content into the document, you must start from the perspective of dom. For example, insert a p paragraph in p:


I want to insert a p in js

var p = document.createElement("p");
var txt = document.creatTextNode(&#39;hello world&#39;);
var p = document.getElementById("myp");
p.appendChild(p);
p.appendChild(txt);
Copy after login

28.insertBefore() ,: Insert an element in front of the element,

Related articles:

"Java Script DOM Programming Art" reading notes

##"JavaScript DOM Programming Art" Reading Notes DOM Basics_javascript skills

Related videos:

JavaScript&DOM video tutorial

The above is the detailed content of Learn about javascript dom programming together and record notes on practical experience. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template