简体   繁体   English

jQuery完全用另一个DOM替换元素的DOM - 更快的方法?

[英]jQuery complete replace DOM of element with another DOM - faster way?

I'm using jQuery's AJAX for getting new content from server. 我正在使用jQuery的AJAX从服务器获取新内容。 Data is loaded in JSON: 数据以JSON格式加载:

$.ajax({
    url: url,
    data: {
        'ajax': '1',
    },
    dataType: 'json',
    success: somefunction
});

For server-side application limitation, I'm not able to setup more JSON variables inside so I have to load everything into content. 对于服务器端应用程序限制,我无法在内部设置更多JSON变量,因此我必须将所有内容加载到内容中。 That is why I have to load result into jQuery, than search and replace some elements on page, like this (used in somefunction ): 这就是为什么我必须将结果加载到jQuery中,而不是搜索和替换页面上的一些元素,就像这样(在somefunction ):

var somefunction = function(data) {
    var con = $('<div></div>').html(data.content); // just $(data.content) is not working
    $('div#mainContent').html(con.find('div#ajax-content').html());
    ... // same process with three more divs
}

EDIT: Please, note that I have to do same process to replace three divs! 编辑:请注意,我必须做同样的过程来替换三个div!

There is more about that, but as example, it's enough I hope. 关于这一点还有更多,但作为例子,我希望它足够了。 My question: For some logic way, I expect that loading result into DOM ( $(data.content) ), parsing to html ( con.find('dix#ajax-content').html() ) and back to DOM ( $('div#mainContent').html() ) seems to me like loosing some resources and decreasing the perfomance so I would like to know if there is any faster way to do it and load DOM directly, like: 我的问题:对于某种逻辑方式,我希望将结果加载到DOM( $(data.content) ),解析为html( con.find('dix#ajax-content').html() )并返回DOM( $('div#mainContent').html() )在我看来喜欢丢失一些资源并降低性能,所以我想知道是否有更快的方法来直接加载DOM,如:

$('div#mainContent').dom(con.find('div#ajax-content').dom());

I tried to google it but maybe I don't know what to type in. Also jQuery documentation does not helped me a lot. 我试图谷歌它,但也许我不知道该输入什么。此外,jQuery文档并没有帮助我很多。

Some facts: 一些事实:

  • jQuery 1.9.1 jQuery 1.9.1
  • jQuery UI 1.10.3 available jQuery UI 1.10.3可用

Finally, I know that it would be much more better to do something with server-side app to provide more JSON variables, however, I need to write not-so-easy peace of code which is requiring longer time to develop which I don't have right now. 最后,我知道用服务器端应用程序做一些提供更多JSON变量会好得多,但是,我需要编写不那么简单的代码安静,这需要更长的时间来开发我不喜欢我现在有。 Doing it on client side would be temporary solution for now, however, I don't want to decrease performace a lot. 在客户端进行此操作暂时是临时解决方案,但是,我不想大幅降低性能。

Side-question: 侧面的问题:

is it correct to use find() function in this case or there is any better one? 在这种情况下使用find()函数是正确的还是有更好的?

EDIT 2 (not working parsing string) I'm expecting this working but it's not: 编辑2(不工作解析字符串)我期待这个工作,但它不是:

content = '<div id="ajax-title">Pečivo běžné, sladké, slané</div>
<div id="ajax-whereami"><a href="/category/4">Chléba a pečivo</a> » Pečivo běžné, sladké, slané</div>';
$(content);

I'm not sure it this will help: 我不确定这会有所帮助:

$('div#mainContent').replaceWith(con.find('div#ajax-content'))
 .attr("id","mainContent")

Now you don't have to set the html of the element and get the html of the Element you just created from JSON. 现在您不必设置元素的html并获取刚刚从JSON创建的元素的html。 Not sure if this actually is faster but it does skip the 2 html() steps. 不确定这实际上是否更快但它确实跳过了2个html()步骤。

Actually, $(data.content) should work just fine, but you have to keep in mind that the top level elements can only be reached via .filter() instead of .find() . 实际上, $(data.content)应该可以正常工作,但你必须记住, 顶级元素只能通过.filter()而不是.find() If the elements you wish to target are at least one level deeper than the root you should use .find() though; 如果你想要定位的元素至少比根要深一级,你应该使用.find() ; in the examples below you can replace .filter() with .find() where appropriate. 在下面的示例中,您可以在适当的位置使用.find()替换.filter()

var $con = $(data.content);
$('div#mainContent')
  .empty()
  .append($con.filter('div#ajax-content'))
  .append($con.filter('div#another-id'))
  .append($con.filter('div#and-another-id'));

You can also combine the selectors together: 您还可以将选择器组合在一起:

  .append($con.filter('div#ajax-content, div#another-id, div#and-another-id'));

Lastly, since identifiers should only appear once inside a document, you can drop the div part: 最后,由于标识符只应出现在文档中,因此您可以删除div部分:

  .append($con.filter('#ajax-content, #another-id, #and-another-id'));

Update 更新

Okay, it seems that jQuery doesn't evaluate data.content properly when there are newlines in the wrong places; 好的,似乎jQuery在错误的地方有换行符时没有正确评估data.content ; this should work in all cases: 这应该适用于所有情况:

var wrapper = document.createElement('div');
wrapper.innerHTML = data.content;

var $con = $(wrapper);

You could use .load , although I believe it is essentially just a wrapper for the same thing: 你可以使用.load ,虽然我认为它基本上只是同一件事的包装:

$("#mainContent").load(url + " #ajax-content", data);

You can increase performance on the server side by sending only the specific ajax content (less to download and parse) although that may be difficult. 您可以通过仅发送特定的ajax内容(减少下载和解析)来提高服务器端的性能,尽管这可能很困难。

Other than that, your best bet is to use vanilla JS over jQuery, at least for the appending (using innerHTML directly). 除此之外,你最好的选择是使用vanilla JS而不是jQuery,至少对于追加(直接使用innerHTML )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM