dom-traversal

Faster way to select an element with a given id

泄露秘密 提交于 2021-02-05 07:49:46
问题 I have a question. Let's say we have the following html tag: <div id='foo'>I am a div</div> Τhis div exists on the dom(it is not generated by javascript) If I want to use this div in javascript many times which is the better way to do it? store it in a variable like this: var d = $("#foo") or call it every time with jquery?: $("#foo").methodName() Which method does involve less dom traversal? 回答1: The fastest way is: document.getElementById("foo"); Setting this to a variable for reuse will

How to find the closest sibling in jQuery

橙三吉。 提交于 2020-12-08 05:27:38
问题 I am trying to learn jQuery, I have following mark up In a div, I have two texts date and description and two buttons edit and delete. When I click the edit button, I want to get the date and description of that div Here I am trying to get it using parents() selector, how can I use closest() selector here , if it is not possible with the current markup please suggest how can I proceed with closest() selector. $(document).ready(function() { //If i click on edit button . I want to select the

How to find the closest sibling in jQuery

守給你的承諾、 提交于 2020-12-08 05:25:46
问题 I am trying to learn jQuery, I have following mark up In a div, I have two texts date and description and two buttons edit and delete. When I click the edit button, I want to get the date and description of that div Here I am trying to get it using parents() selector, how can I use closest() selector here , if it is not possible with the current markup please suggest how can I proceed with closest() selector. $(document).ready(function() { //If i click on edit button . I want to select the

What do querySelectorAll and getElementsBy* methods return?

你离开我真会死。 提交于 2020-06-29 08:41:41
问题 Do getElementsByClassName (and similar functions like getElementsByTagName and querySelectorAll ) work the same as getElementById or do they return an array of elements? The reason I ask is because I am trying to change the style of all elements using getElementsByClassName . See below. //doesn't work document.getElementsByClassName('myElement').style.size = '100px'; //works document.getElementById('myIdElement').style.size = '100px'; 回答1: Your getElementById() code works since IDs have to be

jquery - finding if element is in variable

自闭症网瘾萝莉.ら 提交于 2019-12-24 08:02:14
问题 im trying to find out how to see if an error is returned from an AJAX request. for testing the request always returns <p id="error">test</p> I thought you could do it like this: //html is the var which contains the result var DOM = $(html); if( $('#error', DOM ).length != 0 ) { alert(html); } but this does not work. Can anyone shine a light on whats going wrong? 回答1: If your HTML isn't wrapped in something (like a div ) then it needs to be. var html = '<div></div><p id="error">ERROR!</p><span

Is it faster to traverse the DOM from a cached selector than to find an ID'd element in the DOM?

可紊 提交于 2019-12-22 10:29:54
问题 There are a lot of questions about whether or not finding an element is faster via class or id or some other selector. I'm not interested in that. I want to know if you have: var link = $(this); //let's say you're in a click handler Is it faster to find the container by doing var container = link.closest('.container'); //assume container is .container or var container = $('#mycontainer'); //assume same element as above I'm asking this question not just for the particular scenario above (ok,

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

狂风中的少年 提交于 2019-12-21 21:50:25
问题 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 find table cell where value is X

ⅰ亾dé卋堺 提交于 2019-12-21 07:34:03
问题 I am trying to find a <td> where the value is 5. It is a calender so there will only be one 5 value. 回答1: You can use filter method: $('td').filter(function(){ return $(this).text() === '5' }) 回答2: 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

Is there a way to combine $(this) with :nth-child?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 01:32:29
问题 I'm in the middle of an .each iteration and wanted to call out the 2nd or 3rd child for the each..but cant make it work. alert($(this + ' :nth-child(2)').attr('id')); My only option that I can think of is something terrible goofy like this: $(this).children(':first').next().attr('id', 'ddParam' + newCount); $(this).children(':first').next().next().attr('id', 'txt' + newCount); $(this).children(':first').next().next().next().attr('id'... 回答1: What you need is context. With context, the

Is there a way to combine $(this) with :nth-child?

点点圈 提交于 2019-12-18 01:32:18
问题 I'm in the middle of an .each iteration and wanted to call out the 2nd or 3rd child for the each..but cant make it work. alert($(this + ' :nth-child(2)').attr('id')); My only option that I can think of is something terrible goofy like this: $(this).children(':first').next().attr('id', 'ddParam' + newCount); $(this).children(':first').next().next().attr('id', 'txt' + newCount); $(this).children(':first').next().next().next().attr('id'... 回答1: What you need is context. With context, the