0

我正在尝试做这个简单的 AppendChild,调用没有成功:

//Create Video and Apply
function makeVid(src,wW,hH,Ref){
    var NewSrc= SrcCollect[Ref];
    var ambVid= document.createElement("VIDEO");
    ambVid.setAttribute("width",wW);
    ambVid.setAttribute("height",hH);
    ambVid.setAttribute("src",src);
    ambVid.setAttribute("controls","controls");
    ambVid.className="ambiVidElement";        
    ambVid.id="ambiVidElement"+Ref;
    var holder = document.getElementById("ambi_vid_wrapper"+Ref);
    holder.appendChild(ambVid);
}

它不会产生错误,即使我使用 try{} 来捕获错误,也没有错误,但是我创建并要附加的元素不会出现在页面上...

4

1 回答 1

0

很奇怪没有错误,你检查过浏览器的 JavaScript 控制台吗?用调试器走过?

下面的一些建议(更改了标有 的行*):

//Create Video and Apply
function makeVid(src,wW,hH,Ref){
    var NewSrc= SrcCollect[Ref];
    var ambVid= document.createElement("video"); // * lower-case
    ambVid.style.width = wW + "px";              // * setting style properties
    ambVid.style.height = hH + "px";             // *
    ambVid.src = src;                            // * `src` is a reflected property on most elements with a `src` attribute
    ambVid.setAttribute("controls","controls");
    ambVid.className="ambiVidElement";        
    ambVid.id="ambiVidElement"+Ref;
    var holder = document.getElementById("ambi_vid_wrapper"+Ref);
    holder.appendChild(ambVid);
}

除了上述之外,我会仔细检查该getElementById调用是否返回了您期望它返回的内容。

将此作为 CW 答案,因为它实际上只是一系列调试建议。

于 2011-07-04T11:29:21.863 回答