"触摸和QUOT;一个DOM元素 [英] "touch" a DOM element

查看:111
本文介绍了"触摸和QUOT;一个DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有方便的触摸DOM元素?我想删除元素并再次插入到相同的位置。这样的东西:

  element.parentNode.removeChild(element).appendChild(element); 

除了appendChild将元素作为最后一个兄弟插入。

解决方案

使用 insertBefore 替代的appendChild。

  var other = element.nextSibling; 

if(other){
other.parentNode.removeChild(element);
other.parentNode.insertBefore(element,other);
} else {
other = element.parentNode;
other.removeChild(element);
other.appendChild(element);
}


Is there a handy way to "touch" a DOM element? I'd like to remove the element and insert it again at the same position. Something like this:

element.parentNode.removeChild(element).appendChild(element);

except that appendChild inserts the element as the last sibling.

解决方案

Use insertBefore instead of appendChild.

var other = element.nextSibling;

if ( other ) {
  other.parentNode.removeChild(element);
  other.parentNode.insertBefore(element,other);
} else {
  other = element.parentNode;
  other.removeChild(element);
  other.appendChild(element);
}

这篇关于"触摸和QUOT;一个DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆