TAILIEUCHUNG - The javascript anthology 101 essential tips tricks hacks - phần 5

chức năng changeBorder (element) { = đến;} var contentDiv = ('nội dung'); = function () {changeBorder ("đỏ");}; = function () {changeBorder ('đen') | Creating Elements and Text Nodes File excerpt p id starLinks a href Sirius a p We can use DOM methods to create and insert another link at the end of the paragraph File excerpt var anchorText monoceros var newAnchor a anchorText var parent starLinks var newChild newAnchor The value of the variable newChild will be a reference to the newly inserted element. If we were to translate the state of the DOM after this code had executed into HTML code it would look like this p id starLinks a href Sirius a a monoceros a p We didn t specify any attributes for the new element so it doesn t link anywhere at the moment. The process for specifying attributes is explained shortly in Reading and Writing the Attributes of an Element . Discussion There are three basic ways by which a new element or text node can be inserted into a web page. The approach you use will depend upon the point at which you want the new node to be inserted as the last child of an element before another node or as the replacement for a node. The process of appending an element as the last child was explained above. You can insert the node before an existing node using the insertBefore method of its parent element and you can replace a node using the replaceChild method of its parent element. Order the print version of this book to get all 588 pages 89 Chapter 5 Navigating the Document Object Model In order to use insertBefore you need to have references to the node you re going to insert and to the node before which you wish to insert it. Consider this HtMl code File excerpt p id starLinks a id sirius href Sirius a p We can insert a new link before the existing one by calling insertBefore from its parent element the paragraph File excerpt var anchorText .

TỪ KHÓA LIÊN QUAN
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.