Home  >  Article  >  Web Front-end  >  Learn about javascript dom programming together and record notes on practical experience

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

php是最好的语言
php是最好的语言Original
2018-07-27 15:41:371299browse

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>

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            文本节点

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

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

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;
        }
    }
}

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();
  }
  }
}

26.createTextNode is used to create text nodes

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

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:
a5fdcf67963356bc388e8ab959c1fe2594b3e26ee717c64999d7867364b1b4a3
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);

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!

Statement:
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