开发者

How can I use jQuery for messing with a particular div, but not in the current document - in a variable, that contains HTML?

开发者 https://www.devze.com 2022-12-27 13:25 出处:网络
How can I use jQuery for messing with a particular div, but not in the current document - in a variable, that contains HTML?

How can I use jQuery for messing with a particular div, but not in the current document - in a variable, that contains HTML?

The point is that I want to show a preview of a page (a piece of it's content) in a modal window, when the link to this page is clicked. Well, onClick I load this whole HTML into a variable via JSON and then... how would I find a particular div I need in it? It's gonna be almost impossible to parse it with PHP before converting it into JSON and giving back to jQuery processor because of a deep hierarchy. Basically, is it even possible to do smth like $( 'div#some-id' ).blabla(); not for the current document, but for the document, stored in a variable?

Thx 开发者_如何学编程everyone in advance.


Use the optional context argument to the jquery calls. In other words,

$('div#some-id', document).blabla();


You could also do it like this (which is similar to what @nsayer wrote:

$.get(url, {parameters}, function(data){
    $('#somePartOfThePage', data);
});


If you're using the jQuery $.ajax function, you can do something like this...

$.ajax({
  success: function(responseText) {
    $("#previewDiv").html($("div#content", responseText).html());
  }
});

The "success" method will return html (or whatever you set the $.ajax dataType property to), in this case I've given it the name "responseText" (though you can put in any valid variable name here). Then, I've searched through the responseText, grabbed the html of the #content div, and inserted it into the preview div.

Does that sound like what you were wanting to do?


jQuery's .load() can load page fragments into an element:

$('#modalWindow').load('http://example.com/ #content');
0

精彩评论

暂无评论...
验证码 换一张
取 消