---
title: "javascript中mergeAttributes 方法的介绍"
author: "liyanhui"
date: "2009-12-31T17:32:06Z"
tags: ["JavaScript"]
categories: ["Web前端"]
slug: "javascript-mergeattributes"
draft: false
lastmod: "2009-12-31T17:49:46Z"
id: 560724
url: "https://liyanhui.iteye.com/blog/560724"
---

介绍一下mergeAttributes 方法。它有两个参数oSource与 bPreserve ： mergeAttributes oSource IE5.0 必选项。对象(Element)。指定属性要被复制到 object 的对象。 bPreserve IE5.5 可选项。布尔值(Boolean)。 false :　 不保护目标对象的身份属性(id,name)。 true : 默认值。保护目标对象 object 的身份属性。 mergeAttributes方法能把oSource 对象的所有属性，包括自定义属性，依附在其上面的事件与样式都复制过，在 IE5.0 及之前的版本中，只读属性不会被复制。如 id 属性。 在 IE5.5+ 中，通过设置 bPreserve 参数值为 false ，可以使 oSource 的所有属性被复制到 调用此方法的对象上 ，包括id属性和 name属性。 1.//下面代码会把新生成的input对象的name属性复制到inputObject对象上。 2.inputObject.mergeAttributes(document.createElement("")) 下面是一个完美的设置属性的方案 var setAttribute = function(obj,attrName,attrValue){ if("!+\\v1" && attrName === "name"){ obj.mergeAttributes(document.createElement(""), false); }else{ if(obj.setAttribute){ obj.setAttribute(attrName,attrValue); }else{ obj\[attrName] = attrValue; } } } 3.//mergeAttributes 复制所有读/写标签属性到指定元素。

This is a sample **DIV** element.

This is another sample **DIV** element.