prepend

How to insert element as a first child?

牧云@^-^@ 提交于 2019-11-28 07:54:11
I want to add a div as a first element using jquery on each click of a button <div id='parent-div'> <!--insert element as a first child here ...--> <div class='child-div'>some text</div> <div class='child-div'>some text</div> <div class='child-div'>some text</div> </div> Mohamed Nuur Try the $.prepend() function. Usage $("#parent-div").prepend("<div class='child-div'>some text</div>"); Demo var i = 0; $(document).ready(function () { $('.add').on('click', function (event) { var html = "<div class='child-div'>some text " + i++ + "</div>"; $("#parent-div").prepend(html); }); }); <script src=

jQuery move node out of its parent

旧时模样 提交于 2019-11-28 01:23:08
I have this code: <ul class="list"> <li> <a href="#" > <img src="IMAGE" /> SOME TEXT </a> </li> <li> <a href="#" > <img src="ANOTHER IMAGE" /> SOME DIFFERENT TEXT </a> </li> </ul> I want the images prepended to the parent node like this: <ul class="list"> <li> <img src="IMAGE" /> <a href="#" > SOME TEXT </a> </li> <li> <img src="ANOTHER IMAGE" /> <a href="#" > SOME DIFFERENT TEXT </a> </li> </ul> Try this: $('.list > li > a > img').each(function() { $(this).insertBefore($(this).parent()); }) Demo at http://jsfiddle.net/alnitak/3nEVz/ EDIT I came up with a cleaner version: $('.list > li > a >

Prepend a single line to file with Ruby

丶灬走出姿态 提交于 2019-11-27 20:57:19
问题 I'd like to add a single line to the top a of file with Ruby like this: # initial file contents something else # file contents after prepending "hello" on its own line hello something else The following code just replaces the contents of the entire file: f = File.new('myfile', 'w') f.write "test string" 回答1: This is a pretty common task: original_file = './original_file' new_file = original_file + '.new' Set up the test: File.open(original_file, 'w') do |fo| %w[something else].each { |w| fo

Unix command to prepend text to a file

邮差的信 提交于 2019-11-27 17:16:29
Is there a Unix command to prepend some string data to a text file? Something like: prepend "to be prepended" text.txt Prince John Wesley sed -i.old '1s;^;to be prepended;' inFile -i writes the change in place and take a backup if any extension is given. (In this case, .old ) 1s;^;to be prepended; substitutes the beginning of the first line by the given replacement string, using ; as a command delimiter. shime printf '%s\n%s\n' "to be prepended" "$(cat text.txt)" >text.txt Process Substitution I'm surprised no one mentioned this. cat <(echo "before") text.txt > newfile.txt which is arguably

Most efficient way to prepend a value to an array

≯℡__Kan透↙ 提交于 2019-11-27 10:10:48
Assuming I have an array that has a size of N (where N > 0 ), is there a more efficient way of prepending to the array that would not require O(N + 1) steps? In code, essentially, what I currently am doing is function prependArray(value, oldArray) { var newArray = new Array(value); for(var i = 0; i < oldArray.length; ++i) { newArray.push(oldArray[i]); } return newArray; } maerics I'm not sure about more efficient in terms of big-O but certainly using the unshift method is more concise: var a = [1, 2, 3, 4]; a.unshift(0); a; // => [0, 1, 2, 3, 4] [Edit] This jsPerf benchmark shows that unshift

Prepending Data to a File

爱⌒轻易说出口 提交于 2019-11-27 07:56:59
问题 There's no way in any operating system I'm aware of for a program to prepend data to a file efficiently. And yet, this doesn't seem difficult -- the file system can add another extent to a file descriptor if needed. So the question is, why don't operating systems implement this (rather trivial) operation? 回答1: I don't think it's as easy as you suggest. It's true that the file-system could allocate a new block, store the prepended data in it, change the file pointer to point to that block and

jquery - keep window from changing scroll position while prepending items to a list?

江枫思渺然 提交于 2019-11-27 06:58:51
I have a page that displays messages and I want it to work just like Facebook, but without the lazy loader. Messages are displayed in chronological order, most recent last. My message list is initially populated x number of most recent messages, and the window is scrolled to the bottom. When the user starts to read the thread, they read from bottom to top. If they get to the top, they can load more messages... I make them click a button... facebook has a lazy loader. New messages are prepended to the list. Problem: As the new messages are prepended, the existing messages are pushed down,

What's the idiomatic syntax for prepending to a short python list?

这一生的挚爱 提交于 2019-11-27 05:54:55
list.append() is the obvious choice for adding to the end of a list. Here's a reasonable explanation for the missing list.prepend() . Assuming my list is short and performance concerns are negligible, is list.insert(0, x) or list[0:0] = [x] idiomatic? The s.insert(0, x) form is the most common. Whenever you see it though, it may be time to consider using a collections.deque instead of a list. Nil Geisweiller If you can go the functional way, the following is pretty clear new_list = [x] + your_list Of course you haven't inserted x into your_list , rather you have created a new list with x

Most efficient way to prepend a value to an array

廉价感情. 提交于 2019-11-27 04:01:43
问题 Assuming I have an array that has a size of N (where N > 0 ), is there a more efficient way of prepending to the array that would not require O(N + 1) steps? In code, essentially, what I currently am doing is function prependArray(value, oldArray) { var newArray = new Array(value); for(var i = 0; i < oldArray.length; ++i) { newArray.push(oldArray[i]); } return newArray; } 回答1: I'm not sure about more efficient in terms of big-O but certainly using the unshift method is more concise: var a =

.append(), prepend(), .after() and .before()

天涯浪子 提交于 2019-11-26 23:14:27
I am pretty proficient with coding, but now and then I come across code that seems to do basically the same thing. My main question here is, why would you use .append() rather then .after() or vice verses? I have been looking and cannot seem to find a clear definition of the differences between the two and when to use them and when not to. What are the benefits of one over the other and also why would i use one rather then the other?? Can someone please explain this to me? var txt = $('#' + id + ' span:first').html(); $('#' + id + ' a.append').live('click', function (e) { e.preventDefault(); $