dom-traversal

How to write a simple preorder DOM tree traversal algorithm in jQuery?

帅比萌擦擦* 提交于 2019-12-04 17:15:16
I'd like to take the code found here: http://www.jslab.dk/articles/non.recursive.preorder.traversal.part2 // HTML element var root = document.documentElement; recursivePreorder(root); // Recusively find and handle all text nodes function recursivePreorder(node) { // If node is a text node if (node.type == 3) { // Do something with node } // else recurse for each child node else { for(var i=0; i<node.childNodes.length; i++) recursivePreorder(node.childNodes[i]); } } and convert it into clean jQuery. Any idea? I know recursion requires argument.callee since the callbacks in jQuery are anonymous,

Is the relation between <iframe> and it's document one way?

醉酒当歌 提交于 2019-12-04 12:57:55
You can get the nodes of the document object of an <iframe> with the contentDocument property of the HTMLIFrameElement But I can't find a way getting the <iframe> back out of the node. Is the relation between the <iframe> and it's document is only one way? If it does, WHY? A non working DEMO: var iframe = document.getElementById('iframe'); var doc = iframe.contentDocument || iframe.contentWindow.document; var div = doc.getElementsByTagName('div')[0]; console.log('Did we find the iframe? ' + ($(div).closest('iframe').length > 0));​ // Output: "Did we find the iframe? false" The iframe element

remove 3 last divs with jQuery

时光总嘲笑我的痴心妄想 提交于 2019-12-04 02:57:50
<div id="widgetAreaFooter"> <div class="row">1</div> <div class="row">2</div> <div class="row">3</div> <div class="row">4</div> <div class="row">5</div> <div class="row">6</div> <div class="row">7</div> </div> How to remove the 3 last div ? I tryed this but it doesn't work :/ var row = $( '#widgetAreaFooter>.row' ); var nbr = row.length ; for ( var i=4;i<nbr;i++ ) row.get(i).remove(); or for ( var i=4;i<nbr;i++ ) row[i].remove(); This will remove the last three elements: $('#widgetAreaFooter > .row').slice(-3).remove(); jsFiddle Demo You can get a part of a jQuery collection using .slice() .

Jquery find table cell where value is X

六月ゝ 毕业季﹏ 提交于 2019-12-04 02:19:11
I am trying to find a <td> where the value is 5. It is a calender so there will only be one 5 value. You can use filter method: $('td').filter(function(){ return $(this).text() === '5' }) Use the :contains selector: var td = $("td:contains('5')"); Edit: This will also select the td with 15 and 25 , if you want exact 5 , then use the .filter method as the other answer said. 来源: https://stackoverflow.com/questions/12746846/jquery-find-table-cell-where-value-is-x

Javascript-HTML - how to iterate through all the forms on a page?

偶尔善良 提交于 2019-12-03 15:25:48
How can I iterate through all forms in a document using javascript? The code below will go through an html document, get all forms and do a pop-up alert of the names of each form. var formsCollection = document.getElementsByTagName("form"); for(var i=0;i<formsCollection.length;i++) { alert(formsCollection[i].name); } This is just a start to see if you are getting the reult you require. Thereafter, remove the alert and continue to do what you need to. rahul You can use document.forms collection See forms Collection Here's an example using the document.forms instead of getElementsByTagName(). As

What's the difference between .closest() and .parents('selector')?

南笙酒味 提交于 2019-12-03 11:02:55
What's the difference between these? Is one more efficient than the other? I'm a little bit confused to why they both exist. Say I have this markup: <table> <tr> <td>...</td> <td><span class='toggle'>Toggle</span></td> </tr> <tr> <td>...</td> <td><span class='toggle'>Toggle</span></td> </tr> <tr> <td>..</td> <td><span class='toggle'>Toggle</span></td> </tr> </table> From the <span> tags I could use either $(this).closest('tr'); or $(this).parents('tr'); to access the parent/closest <tr> tag. parent returns the immediate parents (one for each element in the caller object) or nothing if the

Selecting the firstChild and whitespace issue

泪湿孤枕 提交于 2019-12-02 12:38:40
问题 I have a markup like this: <table id='myTable'> <thead> <tr> <td>Hello</td> </tr> </thead> </table> But when I try to select the firstChild of #table , it returns whitespace (as it is expected). var ss = document.getElementById("myTable"); console.log(ss.firstChild); And it returns: <TextNode textContent="\n "> How should I filter whitespace in getting child nodes? 回答1: Look at the article here http://www.sitepoint.com/removing-useless-nodes-from-the-dom/ This guy has given a JS function that

How can I swap the form values of table row1 by the form values of table row2 without changing the table rows using javascript?

岁酱吖の 提交于 2019-12-02 08:22:16
问题 I am trying to swap the form values in row1 with the form values of row2 without swapping the rows. Can someone show me away to achieve this in pure Javascript, vanilla JS, or jQuery. I made the table rows shorter with just two rows, but the actual table consists of 17 rows. Please look very closely at the ids and form values in the third example. When the UP or DOWN button is not click, the table looks like this in simple form: <form id="menuitems"> <table class="toolbaritems"> <tbody class=

Jquery find nearest matching element

筅森魡賤 提交于 2019-11-30 12:57:16
问题 I have a series of rows with columns and I want to select the value of an input field that is in a previous column to the input field (price input) that I am calling a function on when a key is released. I have tried: quantity = $(this).parent().parent().children().val() ; quantity = $(this).parent().parent().children().closest('.inputQty', this).val() ; But neither work. An example of the DOM: <div class="row"> <div class="column"><input class="inputQty" id="quantity0" /></div> <div class=

Jquery find nearest matching element

こ雲淡風輕ζ 提交于 2019-11-30 04:37:28
I have a series of rows with columns and I want to select the value of an input field that is in a previous column to the input field (price input) that I am calling a function on when a key is released. I have tried: quantity = $(this).parent().parent().children().val() ; quantity = $(this).parent().parent().children().closest('.inputQty', this).val() ; But neither work. An example of the DOM: <div class="row"> <div class="column"><input class="inputQty" id="quantity0" /></div> <div class="column"><input class="someOther" id="Other0" /></div> <div class="column"> <div class="cSelect"> <select