2 xy xiaomo xy_xiaomo 于 2014.12.03 17:17 提问

js有什么方法代替cloneNode的写法么?

/**
* 鼠标事件:处理下拉框内容显示不全(在遮罩div中无法正常显示效果)
* @param selectObj
/
function FixWidth(selectObj) {
var newSelectObj = document.createElement("select");
*
_newSelectObj = selectObj.cloneNode(true);_**
newSelectObj.selectedIndex = selectObj.selectedIndex;
newSelectObj.id = "newSelectObj";

    var e = selectObj;
    var absTop = e.offsetTop;
    var absLeft = e.offsetLeft;
    while (e = e.offsetParent) {
        absTop += e.offsetTop;
        absLeft += e.offsetLeft;
    }
    with (newSelectObj.style) {
        position = "absolute";
        top = absTop + "px";
        left = absLeft + "px";
        width = "auto";
    }

    var rollback = function() {
        RollbackWidth(selectObj, newSelectObj);
    };
    if (window.addEventListener) {
        newSelectObj.addEventListener("blur", rollback, false);
        newSelectObj.addEventListener("change", rollback, false);
    } else {
        newSelectObj.attachEvent("onblur", rollback);
        newSelectObj.attachEvent("onchange", rollback);
    }

    selectObj.style.visibility = "hidden";
    document.body.appendChild(newSelectObj);

    var newDiv = document.createElement("div");
    with (newDiv.style) {
        position = "absolute";
        top = (absTop - 10) + "px";
        left = (absLeft - 10) + "px";
        width = newSelectObj.offsetWidth + 20;
        height = newSelectObj.offsetHeight + 20;
        background = "transparent";
    }
    document.body.appendChild(newDiv);
    newSelectObj.focus();
    var enterSel = "false";
    var enter = function() {
        enterSel = enterSelect();
    };
    newSelectObj.onmouseover = enter;

    var leavDiv = "false";
    var leave = function() {
        leavDiv = leaveNewDiv(selectObj, newSelectObj, newDiv, enterSel);
    };
    newDiv.onmouseleave = leave;
}

function RollbackWidth(selectObj, newSelectObj) {
    selectObj.selectedIndex = newSelectObj.selectedIndex;
    selectObj.style.visibility = "visible";
    if (document.getElementById("newSelectObj") != null) {
        document.body.removeChild(newSelectObj);
    }
}

function removeNewDiv(newDiv) {
    document.body.removeChild(newDiv);
}

function enterSelect() {
    return "true";
}

function leaveNewDiv(selectObj, newSelectObj, newDiv, enterSel) {
    if (enterSel == "true") {
        RollbackWidth(selectObj, newSelectObj);
        removeNewDiv(newDiv);
    }
}

1个回答

lzp_lrp
lzp_lrp   Ds   Rxr 2015.01.09 08:41

在js里cloneNode可以直接使用,为什么要替代它呢?

cloneNode方法是克隆节点用的,自己写代码也可以实现

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!