dom-manipulation

Appending a DOM element twice (jQuery)

谁都会走 提交于 2019-12-04 00:30:18
Can someone explain why the following snippet does not add <foo> to both #a and #b ? HTML: <div id="a"></div> <div id="b"></div> JS: $(function(){ var $foo = $("<foo>HI</foo>"); $("#a").append($foo); $("#b").append($foo); }); jsfiddle Edit: thanks for the helpful points, the fact that .append() moves the element explains this behavior. Since the element in my application is actually a Backbone View's .el , I prefer not to clone it. Because using append actually moves the element. So your code was moving $foo into the document at #a , then moving it from #a to #b . You could clone it instead

How can I Observe the contents of an 'a' tag - jquery

做~自己de王妃 提交于 2019-12-03 16:29:30
问题 I have a blank <a> tag that content is loaded into via an external piece of javascript. I want to observe the <a> and when its content changes perform another task. The content will only ever change once. Can this be done? I am using also using jQuery. Thanks in advance 回答1: You can use a mixture out of jQuery && DOM Level 3 events (see browser support below). If you want to check for any changes within the content , you could do this: var $a = $('a'); $a.one('DOMNodeInserted', function(e) {

How to add boolean attribute using JavaScript

二次信任 提交于 2019-12-03 16:23:02
问题 How do you add boolean attributes using JavaScript? For example, how can you change: <p> to <p contenteditable> <p> to <p data-example> 回答1: In general, you can use element.setAttribute('attributeName', 'value') or element.propertyName = value to toggle an element’s attributes or properties. Boolean attributes For boolean attributes, set the attribute with the same-named value: element.setAttribute('disabled', 'disabled'); Removing a boolean attribute works the same way as other attributes:

Create a label using jQuery on the fly

我们两清 提交于 2019-12-03 10:33:53
I need to create a label and text field on the fly and also include datepicker for the textfield. I need something like this: <label for="from">From </label> <input type="text" id="from" name="from" /> I have tried something like this in jQuery: var label = $("<label>").attr('for', "from"); label.html('Date: ' + '<input type="text" id="from name="from" value="">'); $(function() { $("#from").datepicker(); }); This one somehow doesn't create the label and also the text field. I am not sure what I am missing. EDIT To be more precise, I am using this in the portlets and I don't have body tags in

How to add boolean attribute using JavaScript

北城以北 提交于 2019-12-03 05:46:05
How do you add boolean attributes using JavaScript? For example, how can you change: <p> to <p contenteditable> <p> to <p data-example> In general, you can use element.setAttribute('attributeName', 'value') or element.propertyName = value to toggle an element’s attributes or properties. Boolean attributes For boolean attributes, set the attribute with the same-named value: element.setAttribute('disabled', 'disabled'); Removing a boolean attribute works the same way as other attributes: element.removeAttribute('disabled'); However, neither of your two examples are boolean attributes !

Angular apply class in directive

本秂侑毒 提交于 2019-12-02 06:58:36
I have a angular directive which will produce bootstrap form-group, looks for $scope.errors for value of ng-model of the directive to show errors.. Example below: My html code: <input type="text" b-input ng-model="data.company.name" label="Company Name" class="form-control"/> and my directive code: app.directive('bInput', function ($compile) { return { restrict: 'EA', replace: true, link: function (scope, element, attrs) { var div = $('<div>', { 'class': 'form-group', 'ng-class': " 'has-error' : errors." + attrs.ngModel + " != null " }); $compile(div)(scope); element.wrap(div); if (attrs.label

Bad performance IE using documentFragment

扶醉桌前 提交于 2019-12-01 22:17:44
问题 To test DOM manipulation versus innerHTML I deviced this little test method using a documentFragment (web page) to append 10000 href elements to a div element. The performance is ok for Chrome or Firefox, but in IE (10, 9, 8) it's dramatically bad, it takes around 10-12 seconds . Can anyone explain this difference and/or elaborate on a solution to enhance performance for IE? Here's a jsfiddle demonstrating it. The method: function useFragment(){ var frag = document.createDocumentFragment(), i

Update the DOM vs Rerender the View In Angular

走远了吗. 提交于 2019-12-01 05:16:10
I am following https://docs.angularjs.org/guide/scope . 5.The $watch list detects a change on the name property and notifies the interpolation, which in turn updates the DOM. 6.Angular exits the execution context, which in turn exits the keydown event and with it the JavaScript execution context. 7.The browser re-renders the view with update text. I am having doubt what is the differnce between updates the DOM on Line 5 and browser re-renders the view on line 7. Thanks In advance. The DOM represents the HTML document loaded in the browser. JavaScript can manipulate the document through the DOM

Update the DOM vs Rerender the View In Angular

前提是你 提交于 2019-12-01 02:44:16
问题 I am following https://docs.angularjs.org/guide/scope. 5.The $watch list detects a change on the name property and notifies the interpolation, which in turn updates the DOM. 6.Angular exits the execution context, which in turn exits the keydown event and with it the JavaScript execution context. 7.The browser re-renders the view with update text. I am having doubt what is the differnce between updates the DOM on Line 5 and browser re-renders the view on line 7. Thanks In advance. 回答1: The DOM

replace one div with another on hover

旧城冷巷雨未停 提交于 2019-11-30 21:26:05
I want to replace one div with another when hovering over it. Specifically there will be an average in words, such as "struggling" or "exceeding expectations", and I want to replace this with the numerical average when the user hovers over the average in words. #html <div class="switch avg_words float_left"> A+ (hover to see score) </div> <div class="avg_num"> AVG = 98.35% </div> #css .avg_num { display: none; } #jquery $('.switch').hover(function() { $(this).closest('.avg_words').hide(); $(this).next('div').closest('.avg_num').show(); }, function() { $(this).next('div').closest('.avg_num')