dom-traversal

jQuery Wrap a div from H2 until last paragraph

早过忘川 提交于 2019-12-08 03:59:47
问题 I want to wrap a <div> FROM THE BEGINNING of the <H2> up until the next <H2> , but it is only starting on the first paragraph. This is what I have, which ALMOST does the job: $('h2:contains("test")').nextUntil('h2').wrapAll('<div class="wrapper" />'); Here's my HTML: /* from here */ <h2 class='test'>Include this in the wrap</h2> <p>this</p> <p>and this</p> <p>and this</p> /* to here */ <h2 class='next'>before this</h2> 回答1: I would try: $("h2.test").nextUntil("h2").andSelf().wrapAll('<div

Faster way of replacing text in all dom elements?

邮差的信 提交于 2019-12-07 05:27:22
问题 I'm trying to replace all text in between tags and I want to know the fastest way of doing so. An example would be trying to replace all text with the arbitrary string helloWorld, so that this: <div> <div> RandomText1 <div> RandomText2 </div> </div> </div> Becomes this: <div> <div> helloWorld <div> helloWorld </div> </div> </div> My current approach would be : Do Depth-first search (DFS) on DOM For each element parse and determine which part is text and which part is an element. For the part

difference between using MULTIPLE 'id' and 'class' attributes in HTML and CSS

大憨熊 提交于 2019-12-06 08:43:14
According to CSS principles when we want to implement reusability of styles we should use class attribute and when we know that there is an unique element in whole DOM structure we should use id attribute to that Element and then specify a style. But in this era of Web Applications, DOM structure can be too complex and there is a possibility of duplicate id . Best example would be #title . Its kind of name which can appear anywhere in the document. Now the best part is if I use #title or .title while defining styles (assuming that they have been appeared more than once and have different

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

[亡魂溺海] 提交于 2019-12-06 08:25:39
问题 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? ' + (

jQuery Wrap a div from H2 until last paragraph

柔情痞子 提交于 2019-12-06 06:11:09
I want to wrap a <div> FROM THE BEGINNING of the <H2> up until the next <H2> , but it is only starting on the first paragraph. This is what I have, which ALMOST does the job: $('h2:contains("test")').nextUntil('h2').wrapAll('<div class="wrapper" />'); Here's my HTML: /* from here */ <h2 class='test'>Include this in the wrap</h2> <p>this</p> <p>and this</p> <p>and this</p> /* to here */ <h2 class='next'>before this</h2> I would try: $("h2.test").nextUntil("h2").andSelf().wrapAll('<div class="wrapper" />'); It does seem to do the trick. 来源: https://stackoverflow.com/questions/7628339/jquery-wrap

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

主宰稳场 提交于 2019-12-06 02:21:39
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, well, yes, for this scenario too) but for cached traversal vs. creating a fresh jQuery object that has

Selecting all divs except one

送分小仙女□ 提交于 2019-12-06 00:41:22
<div id="main"> <div class="a"></div> <div class="b"><p>not me</p></div> <div class="b"></div> <div class="b"></div> <div class="c"></div> </div> How we can write a selector to select all divs with class b except whose child is <p>not me</p> ? $('div.b:not(:has(p))')......... Or the readable version $('div.b').filter(function(){ return !$(this).find('p').length; }); If you want to match the content as well: $('div.b').filter(function(){ return $(this).find('p').text() !== "not me"; }); Live DEMO demo http://jsfiddle.net/46nC5/1/ Since you are specifically looking for class b made a demo for

Faster way of replacing text in all dom elements?

二次信任 提交于 2019-12-05 11:37:04
I'm trying to replace all text in between tags and I want to know the fastest way of doing so. An example would be trying to replace all text with the arbitrary string helloWorld, so that this: <div> <div> RandomText1 <div> RandomText2 </div> </div> </div> Becomes this: <div> <div> helloWorld <div> helloWorld </div> </div> </div> My current approach would be : Do Depth-first search (DFS) on DOM For each element parse and determine which part is text and which part is an element. For the part that is text replace it. This to me would be really slow, especially trying to do this for a large

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

非 Y 不嫁゛ 提交于 2019-12-04 21:25:56
问题 How can I iterate through all forms in a document using javascript? 回答1: 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. 回答2: You can use document.forms

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

折月煮酒 提交于 2019-12-04 18:14:31
问题 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.