document.getElementById() VS. getElementById()

前端 未结 6 1442
情书的邮戳
情书的邮戳 2020-12-07 03:43

It is common for me to register javascript functions for certain events by doing something like:

myBtn.Attributes.Add(\"onClick\", \"Validate(getElementById(         


        
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 04:17

    When you use getElementById() and it works that mean that the function where it's called is running on the context of the document, that's is this == document.

    So, you should ALWAYS use document.getElementById to avoid that kind of errors.

    Anyway, I would even stop using getElementById altogether and start using JQuery, i'm sure you'll never regret it.

    Your code would look something like this if you used JQuery:

    $("#myBtnID").click(function () { Validate($("#myTextboxID"))});
    

提交回复
热议问题