简体   繁体   English

用jQuery替换DIV而不删除它

[英]Replacing DIV with jQuery without removing it

I have a page which is generated and structured as a tree - nested DIVs, etc.. While the user views the page it is possible that some DIVs are updated on the server side and the changes are pushed to the client as JSON data, from which a DIV can be generated. 我有一个页面,该页面的生成和结构为树-嵌套的DIV等。当用户查看该页面时,可能会在服务器端更新某些DIV,并将更改作为JSON数据从客户端推送到客户端。可以生成DIV。

My problem is that even though I have the old DIV 我的问题是即使我有旧的DIV

var oldDiv = $('#foo');

and I have a new DIV generated by 我有一个新的DIV由

var newDiv = generateDiv(jsonData);

I need to update the old one (both attributes and it's content) without deleting it. 我需要更新旧的属性(包括属性及其内容)而不删除它。 I was going to use the jQuery method .replaceWith() as such 我打算像这样使用jQuery方法.replaceWith()

oldDiv.replaceWith(newDiv);

but according to the documentation it is implemented as remove&create. 但根据文档,它被实现为remove&create。

The .replaceWith() method removes content from the DOM and inserts new content in its place with a single call. .replaceWith()方法从DOM中删除内容,并通过一次调用将新内容插入其位置。

How can I update the old DIV without removing it? 如何在不删除旧DIV的情况下进行更新? Is there some nice way to do this, or do I need to do it attribute by attribute? 有一些不错的方法可以做到这一点,还是我需要逐个属性地做到这一点?

As you've suggested, you may need to replace the attribute values individually. 如您所建议,您可能需要单独替换属性值。 However, if it reads better, you can actually pass an object to the attr method, and it will update the values you supply. 但是,如果它读起来更好,则实际上可以将一个对象传递给attr方法,它将更新您提供的值。

oldDiv.attr({
    attr1: newDiv.attr1,
    attr2: newDiv.attr2,
    attr3: newDiv.attr3
});

If you wanted to loop through the attributes to build the object, you could do that like this. 如果您想遍历属性来构建对象,则可以这样做。

var newAttributes = {};

$.each(newDiv[0].attributes, function(index, attribute){
   newAttributes[attribute.name] = attribute.value;
});

oldDiv.attr(newAttributes);

It cannot be done since a div element may contain many elements. 由于div元素可能包含许多元素,因此无法完成。 Why dont u just append the new contents into it. 您为什么不将新内容添加到其中呢? You can use jquery's append() method. 您可以使用jquery的append()方法。

$(oldDiv).append("#new_div_id");

It will be appended as a child. 它将作为儿童追加。

If at all you want to update any <p> element, you can use the html() function to get the contents of a 如果您根本想更新任何<p>元素,则可以使用html()函数获取以下内容:

tag and then 标签,然后

old_para_contents=("p").html();
$("p").html(old_para_contents+"New contents");

I've come up with one solution so far, but if anyone comes up with a better one, I will gladly assign it as the correct one. 到目前为止,我已经提出了一种解决方案,但是如果有人提出了更好的解决方案,我将很乐意将其指定为正确的解决方案。 I need to make this as clean as possible. 我需要使它尽可能干净。

var oldDiv = $('#my-old-div');
var newDiv = generateDiv(data);

oldDiv.attr("id", newDiv.attr("id"));
oldDiv.attr("class", newDiv.attr("class"));
//...
oldDiv.html(newDiv.html());

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

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