dom-manipulation

Where does DOM manipulation belong in Angular 2?

谁说我不能喝 提交于 2019-11-27 08:05:10
In Angular 1 all DOM manipulation should be done in directives to ensure proper testability, but what about Angular 2? How has this changed? I've been searching for good articles or any information at all about where to put DOM manipulation and how to think when doing it, but I come up empty every time. Take this component for example (this is really a directive but let's pretend that it's not): export class MyComponent { constructor(private _elementRef: ElementRef) { this.setHeight(); window.addEventListener('resize', (e) => { this.setHeight(); }); } setHeight() { this._elementRef

How to move all HTML element children to another parent using JavaScript?

独自空忆成欢 提交于 2019-11-27 07:31:58
Imagine: <div id="old-parent"> <span>Foo</span> <b>Bar</b> Hello World </div> <div id="new-parent"></div> What JavaScript can be written to move all the child nodes (both elements, and text nodes) from old-parent to new-parent without jQuery? I don't care about whitespace between nodes, though I expect to catch the stray Hello World , they would need to be migrated as-is too. EDIT To be clear, I want to end up with: <div id="old-parent"></div> <div id="new-parent"> <span>Foo</span> <b>Bar</b> Hello World </div> The answer of the question from which this is a proposed duplicate would result in:

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

谁说我不能喝 提交于 2019-11-27 03:35:25
问题 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? 回答1: 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

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

不羁的心 提交于 2019-11-26 23:57:08
问题 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? 回答1: jQuery('#masterdiv div').html(''); 回答2: 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. 回答3: Use jQuery

Where does DOM manipulation belong in Angular 2?

六眼飞鱼酱① 提交于 2019-11-26 17:44:49
问题 In Angular 1 all DOM manipulation should be done in directives to ensure proper testability, but what about Angular 2? How has this changed? I've been searching for good articles or any information at all about where to put DOM manipulation and how to think when doing it, but I come up empty every time. Take this component for example (this is really a directive but let's pretend that it's not): export class MyComponent { constructor(private _elementRef: ElementRef) { this.setHeight(); window

Auto resizing the SELECT element according to selected OPTION's width

落爺英雄遲暮 提交于 2019-11-26 16:32:47
I've got this select element with different option in it. Normally the select element would get its width from the biggest option element, but I want the select element to have the default option value's width which is shorter. When the user selects another option , the select element should resize itself so the whole text is always visible in the element. $(document).ready(function() { $('select').change(function(){ $(this).width($('select option:selected').width()); }); }); Problem: On Chrome (Canary) it always gets a width returned of 0. On Firefox the width get's added and not resized when

How to move all HTML element children to another parent using JavaScript?

亡梦爱人 提交于 2019-11-26 13:22:27
问题 Imagine: <div id="old-parent"> <span>Foo</span> <b>Bar</b> Hello World </div> <div id="new-parent"></div> What JavaScript can be written to move all the child nodes (both elements, and text nodes) from old-parent to new-parent without jQuery? I don't care about whitespace between nodes, though I expect to catch the stray Hello World , they would need to be migrated as-is too. EDIT To be clear, I want to end up with: <div id="old-parent"></div> <div id="new-parent"> <span>Foo</span> <b>Bar</b>

What is innerHTML on input elements?

若如初见. 提交于 2019-11-26 11:23:14
I'm just trying to do this from the chrome console on Wikipedia. I'm placing my cursor in the search bar and then trying to do document.activeElement.innerHTML += "some text" but it doesn't work. I googled around and looked at the other properties and attributes and couldn't figure out what I was doing wrong. The activeElement selector works fine, it is selecting the correct element. Edit : I just found that it's the value property. So I'd like to change what I'm asking. Why doesn't changing innerHTML work on input elements? Why do they have that property if I can't do anything with it?

jQuery : remove element except inside element

回眸只為那壹抹淺笑 提交于 2019-11-26 04:56:53
问题 Is there any way to remove element except inside element: <div class=\"gallery\"> <a href=\"images/rep.png\" title=\"rep\"> <img src=\"http://example.com/uploads/rep.png\" class=\"thumbnail\" alt=\"rep\" title=\"rep\"> </a> </div> to <div class=\"gallery\"> <img src=\"http://example.com/uploads/rep.png\" class=\"thumbnail\" alt=\"rep\" title=\"rep\"> </div> I wrote this code but not work: $(\".gallery\").contents().filter(\".thumbnail\").remove(); 回答1: jQuery has an unwrap() method which

Auto resizing the SELECT element according to selected OPTION&#39;s width

北慕城南 提交于 2019-11-26 04:49:18
问题 I\'ve got this select element with different option in it. Normally the select element would get its width from the biggest option element, but I want the select element to have the default option value\'s width which is shorter. When the user selects another option , the select element should resize itself so the whole text is always visible in the element. $(document).ready(function() { $(\'select\').change(function(){ $(this).width($(\'select option:selected\').width()); }); }); Problem: