dom-manipulation

jQuery - cannot bind events to dynamic elements?

人盡茶涼 提交于 2019-12-19 07:55:26
问题 I've come to maintain a piece of javascript that downloads some JSON data from the server, builds a new table row (like $('<tr></tr') ) and inserts it into the document. The a node is, at one point created like this: var a = $('<a class="foo" href="#"></a>'); and later, an event is bound to it like this: a.click(function () { // yadda yadda return false; }); The only problem is that this doesn't seem to work. Neither does binding through on() or the deprecated live() . The handler is simply

Load AngularJS Template within page, dynamically

老子叫甜甜 提交于 2019-12-18 10:24:40
问题 I have a page containing 2 controllers: one which manages a list of so-called 'apps', and another that is to place the new Angular template into the innerHTML of its Div element. <div ng-controller="appList"></div> <div ng-controller="appPane"> Dynamic content should be loaded here! </div> I have tried using the standard {{expression}} bindings, but they do not work with html, I have also tried the ng-bind-html-unsafe directive (Binding the innerhtml to that of the App request's return) but

jQuery add closing tag and then reopen when using .before()

时光总嘲笑我的痴心妄想 提交于 2019-12-18 06:57:14
问题 I have this HTML: <ul> <div> <li>a</li> <li>b</li> <li class="break">c</li> <li>d</li> <li>f</li> </div> </ul> What I want is where class="break" I want the parent div closed and a new one opened so I end up with this: <ul> <div> <li>a</li> <li>b</li> </div> <div> <li>c</li> <li>d</li> <li>f</li> </div> </ul> Now I've tried this: $(".break").before("</div><div>"); but jQuery doesn't act as I'd expect and instead rewrites what I have entered and puts an empty div instead. Like so: <ul> <div>

Jquery - run function when DOM element is added

假如想象 提交于 2019-12-18 05:18:22
问题 I have a website with jQuery and jQuery UI. I have a Javascript function: function ToButton() { $(".ToButton").button(); } This function runing after the page load and change all elements with class "ToButton". Also I change HTML code when user press a button. E.g.: $("body").append('<span class="ToButton">Test Button</span>'); And I want to run ToButton function to this new element too, but live or delegate methods in jQuery don't have "show" or "create" event type. So I need call function

jQuery empty() vs remove()

偶尔善良 提交于 2019-12-17 21:41:46
问题 What's the difference between empty() and remove() methods in jQuery , and when we call any of these methods, the objects being created will be destroyed and memory released? 回答1: empty() will remove all the contents of the selection. remove() will remove the selection and its contents. Consider: <div> <p><strong>foo</strong></p> </div> $('p').empty(); // --> "<div><p></p></div>" // whereas, $('p').remove(); // --> "<div></div>" Both of them remove the DOM objects and should release the

Loading DIV content via ajax as HTML

孤街浪徒 提交于 2019-12-17 21:12:55
问题 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,

Creating tables dynamically in jQuery

送分小仙女□ 提交于 2019-12-17 20:56:50
问题 I'm building some <table> data dynamically using jQuery, but I'm getting the following error: Uncaught Error: HIERARCHY_REQUEST_ERR: DOM Exception 3 This happens at the appendTo part of a script that looks like this: $('<tr />').append( /* lots of stuff */ ).add( $('<tr />') ).append( /* some more */ ).appendTo($tbody); Where $tbody is $('<tbody />'); Can anyone help me out please? For the sake of completeness, this is the entire code: $('#visitsContainer').show(); $div = $('<div />').css(

How can I highlight a selected list item with jquery?

删除回忆录丶 提交于 2019-12-17 20:35:38
问题 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

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

為{幸葍}努か 提交于 2019-12-17 09:55:08
问题 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. 回答1: 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>')

Change all <img> tag src attributes in html document? [closed]

五迷三道 提交于 2019-12-14 03:36:44
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . Given any html document with any number of <img> tags, and using javascript, how can I change all the src attributes of the <img> tags? For example change all src attributes in <img> tags from "http://example.com/somerandomimage.jpg" to "http://example.com/somerandomimage.jpg?foo