为什么如果我的函数中的范围被 javascript 解释器完全忽略了

Why is this if scope in my function completely ignored by the javascript interpreter?

本文关键字:解释器 javascript 我的 如果 函数 范围 为什么      更新时间:2023-09-26

我有一个函数来实际创建一个HTML元素并快速绑定它。

但是,它不能正常工作。

当我使用 debug(ggCreateElement) 时,我看到它完全跳过了if范围,即使语句是"true"。我已经没有想法了。

function ggCreateElement(tagName,className,idName,appendPointTagName) {
    d=document.createElement(tagName);
    d.className=className;
    d.id=idName;
    ap=document.getElementsByTagName(appendPointTagName);
        if (ap.lenght==0) {
           console.log("Append point tag name is not found ! ") ;
        }
        else {
           ap[0].appendChild(d);
           return d;
    }
}

你能帮帮我吗?谢谢。

代码工作正常,一旦您在 if 条件中更改拼写错误,代码就会返回正确的元素

if (ap.lenght==0)

if (ap.length==0)

另外,你不妨这样做

if (!ap.length)