无法设置未定义的 属性 'class'

Cannot set property 'class' of undefined

我确定我正在做一些愚蠢的事情,或者错过了一些愚蠢的事情,但是我从客户端点击按钮时收到了以下 2 条错误消息:

Cannot set property 'class' of undefined

Cannot set property 'scrollTop' of null

我想要做的就是设计一个 inputTextArea,然后滚动到我拥有的可滚动内容 div 的顶部。

    document.getElementById(x$("#{id:obj1Error}").Attributes["class"] = "has-error");
    var myDiv = document.getElementById(x$("#{id:contentWhiteBackground}"));
    myDiv.scrollTop = 0;

感谢任何指点。干杯

更新:

按钮上的客户端代码:

// Make sure user has entered content into the first objective at minimum
var objcolparent = document.getElementById("ObjColOuter").children[0];
var objValue = objcolparent.getElementsByTagName("TEXTAREA")[0].value;

if(objValue ==""){
var o = {};
o.title = "Validation Failed";
o.body = "You must enter at least one objective before submitting";
o.alertIcon = "fa-thumbs-down fa-lg";
o.alertType = "danger";

var myDiv = document.getElementById("#{id:contentWhiteBackground}");
//document.getElementById("#{id:obj1Error}").className = "has-error";
myDiv.scrollTop = 0;
bootAlert.show('alertServer',JSON.stringify(o));
return false;
}

if(confirm("Are you sure you want to submit your objectives?")){
return true;
}else{
return false;
}

xpage 上的代码,其中包含 obj1Error div:

<xp:repeat id="repeat1" rows="100"
                            value="#{viewScope.fields}" var="fieldName">

                            <xp:text escape="true"
                                id="computedField4" styleClass="h5">


                                <xp:this.value><![CDATA[#{javascript:"Objective "+@RightBack(fieldName, 16)}]]></xp:this.value>
                            </xp:text>
                            <br />
                            <xp:div id="obj1Error">

                                <xp:text escape="true"
                                    id="computedField1" styleClass="h6">



                                    <xp:this.value><![CDATA[#{javascript:"Details"}]]></xp:this.value>
                                </xp:text>
                                <xp:inputTextarea
                                    id="inputTextarea1" style="height:100px;resize:none;"
                                    showReadonlyAsDisabled="true">
                                    <xp:this.value><![CDATA[#{document1[fieldName]}]]></xp:this.value>
                                    <xp:this.disabled><![CDATA[#{javascript:try{

var strStatus:string = document1.getItemValueString("status"); 
var booDisabled:boolean = true;

if(strStatus == "New" || strStatus == "Draft" || strStatus == "Returned") {
booDisabled = false;
}

return booDisabled;

}catch(e){
writeToLog("Error - ccObjectives disable objective details: " + e);
}}]]></xp:this.disabled>

                                </xp:inputTextarea>
                            </xp:div>
                            <br />
                        </xp:repeat>

你只需要写

var myDiv = document.getElementById("#{id:contentWhiteBackground}");

获取 DOM 元素。

要设置元素的 class(es),您必须编写:

document.getElementById("#{id:obj1Error}").className = "has-error";

我建议在 xpages 中使用 dojo toolkit 而不是原生 javascript:

dojo.addClass('#{id:obj1Error}','has-error');
var myDiv = dojo.byId('#{id:contentWhiteBackground}');
myDiv.scrollTop = 0;