简体   繁体   English

无法设置未定义的属性“类”

[英]Cannot set property 'class' of undefined

I'm certain I'm doing something stupid, or missing something silly but I get the follow 2 error messages, from client side onClick of a button:我确定我在做一些愚蠢的事情,或者遗漏了一些愚蠢的事情,但是我从客户端 onClick 一个按钮收到了以下 2 条错误消息:

Cannot set property 'class' of undefined无法设置未定义的属性“类”

Cannot set property 'scrollTop' of null无法将属性“scrollTop”设置为 null

All I'm trying to do is style an inputTextArea, and then scroll to the top of a scrollable content div that I have.我要做的就是为 inputTextArea 设置样式,然后滚动到我拥有的可滚动内容 div 的顶部。

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

Any pointers appreciated.任何指针表示赞赏。 Cheers干杯

UPDATE:更新:

Client code on button:按钮上的客户端代码:

// 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;
}

Code on xpage, which contains the obj1Error div: 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>

You just need to write你只需要写

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

to get the DOM element.获取DOM元素。

To set an element's class(es) you have to write:要设置元素的类,您必须编写:

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

I would recommend to work with the dojo toolkit in xpages instead of native javascript:我建议使用 xpages 中的dojo 工具包而不是原生 javascript:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM