dom-manipulation

Loading DIV content via ajax as HTML

末鹿安然 提交于 2019-11-28 14:20:38
I think I have a very simple question, but I couldn't find a solution. So what I want is to load a DIV content via HTML. The problem is that it is not just a text, but also an image. The ajax call returns the HTML code, but this is displayed in the DIV as HTML and is not executed. Difficult to explain, but obvious from the example... The AJAX call returns the string: <img src="/images/weather/1.png" width="80px"><br>3.3 °C The problem is obvious. Instead of an image and the 3.3°C in the div, I get the actual code, so instead of the image I see img src=.... I know that i need to somehow decode

How can I highlight a selected list item with jquery?

荒凉一梦 提交于 2019-11-28 12:15:55
I have several items in a list and want to highlight the one a user clicks on by applying some css style, maybe a background color etc. My HTML looks like this: <ul class="thumbnails"> <li> <a href="#" class="thumbnail"> <img class="giftthumb" src='thumb1.jpg' alt=""> <span class="gifttitle">Thumb1</span> </a> </li> <li> <a href="#" class="thumbnail"> <img class="giftthumb" src='thumb2.jpg' alt=""> <span class="gifttitle">Thumb3</span> </a> </li> <li> <a href="#" class="thumbnail"> <img class="giftthumb" src='thumb3.jpg' alt=""> <span class="gifttitle">Thumb3</span> </a> </li> </ul> jQUery to

Why is it considered a bad idea to manipulate DOM in controllers?

泄露秘密 提交于 2019-11-28 10:27:18
Many People have told me that it is a very bad thing to manipulate DOM in controllers, but what exactly is the reason. How does it affect the application that you are making? What are the Best Practices to do that and how is it done? Technically controller should be smaller & compact, it should not be playing with a DOM. Controller will only interested to have a business logic & binding level logic that are being called on events. As per my perspective the reason behind " You should not manipulate DOM from the controller " is, It's just because of separation of concern. If you do the DOM

JavaScript to sort DOM elements by some comparator, without jQuery

雨燕双飞 提交于 2019-11-28 03:56:08
问题 Imagine some DOM elements: <ul id="list"> <li data-index="3">Baz</li> <li data-index="1">Foo</li> <li data-index="2">Bar</li> </ul> How can these elements be sorted using JavaScript and without jQuery? Something similar to: document.getElementById('list').sort(function(li) { return li.dataset.index; }); 回答1: You should use the ordering capabilities of flexboxes. This will allow to re-order the elements without moving them around in the DOM. This involves setting the CSS order property. See

How to clear all <div>s’ contents inside a parent <div>?

橙三吉。 提交于 2019-11-28 03:09:52
I have a div <div id="masterdiv"> which has several child <div> s. Example: <div id="masterdiv"> <div id="childdiv1" /> <div id="childdiv2" /> <div id="childdiv3" /> </div> How to clear the contents of all child <div> s inside the master <div> using jQuery? jQuery('#masterdiv div').html(''); Emil Ivanov jQuery's empty() function does just that: $('#masterdiv').empty(); clears the master div . $('#masterdiv div').empty(); clears all the child div s, but leaves the master intact. Use jQuery's CSS selector syntax to select all div elements inside the element with id masterdiv . Then call empty()

How can I remove wrapper (parent element) without removing the child?

北城余情 提交于 2019-11-28 03:09:01
问题 I would like to remove the parent without removing the child - is this possible? HTML structure: <div class="wrapper"> <img src""> </div> <div class="button">Remove wrapper</div> After clicking on the button I would like to have: <img src""> <div class="button">Remove wrapper</div> 回答1: Could use this API: http://api.jquery.com/unwrap/ Demo http://jsfiddle.net/7GrbM/ .unwrap Code will look something on these lines: Sample Code $('.button').click(function(){ $('.wrapper img').unwrap(); }); 回答2

Is there a way to get innerText of only the top element (and ignore the child element's innerText)?

半世苍凉 提交于 2019-11-28 00:13:01
问题 Is there a way to get innerText of only the top element (and ignore the child element's innerText) ? Example: <div> top node text <div> child node text </div> </div> How to get the "top node text" while ignoring "child node text" ? innerText property of top div seem to return concatenation of both inner , top text. 回答1: Just iterate over the child nodes and concatenate text nodes: var el = document.getElementById("your_element_id"), child = el.firstChild, texts = []; while (child) { if (child

Which is better: string html generation or jquery DOM element creation?

。_饼干妹妹 提交于 2019-11-27 19:51:10
Ok, I'm rewriting some vanilla JS functions in my current project, and I'm at a point where there's a lot of HTML being generated for tooltips etc. My question is, is it better/preferred to do this: var html = '<div><span>Some More Stuff</span></div>'; if (someCondition) { html += '<div>Some Conditional Content</div>'; } $('#parent').append(html); OR var html = $('<div/>').append($('<span/>').append('Some More Stuff')); if (someCondition) { html.append($('<div/>').append('Some conditionalContent'); } $('#parent').append(html); ? From a performance standpoint: it depends . In your short example

how to unit test DOM manipulation (with jasmine)

佐手、 提交于 2019-11-27 09:50:35
问题 I need to unit test some DOM manipulation functions with jasmine (currently I run my tests in the browser and with Karma) I was wondering what the best approach would be to do this ? For example, I can mock and stub the window and document objects and spyOn a couple of their functions. But this doesn't really look like an easy solution, so thats why I'm asking this question! Or is there a better way (not using jasmine maybe) to do this ? Thanks a lot 回答1: I've been using a helpful addition to

Change the tag but keep the attributes and content — jQuery/Javascript

 ̄綄美尐妖づ 提交于 2019-11-27 08:58:26
Text changed to <p href="page.html" class="class1 class2" id="thisid">Text</p> I'm familiar with jQuery's replaceWith but that doesn't keep attributes/content as far as I know. Note: Why would p have a href ? Cuz I need to change p back to a on another event. try this: var $a = $('a#thisid'); var ahref = $a.attr('href'); var aclass = $a.attr('class'); var aid = $a.attr('id'); var atext = $a.text(); $a.replaceWith('<p href="'+ ahref +'" class="'+ aclass +'" id="'+ aid+'">'+ atext +'</p>'); Here is a more generic method: // New type of the tag var replacementTag = 'p'; // Replace all a tags with