js缺少对象是怎么回事?在asp.net中的textbox中点击显示日历控件,点击日历会将选中的日期填入textbox

Element.viewportOffset = function(forElement) {
var valueT = 0, valueL = 0;

var element = forElement;
do {
valueT += element.offsetTop || 0;
valueL += element.offsetLeft || 0;

// Safari fix
if (element.offsetParent == document.body &&
element.style['position'] == 'absolute') break;

} while (element = element.offsetParent);

element = forElement;
do {
if (!BrowserType.Opera || element.tagName == 'BODY') {
valueT -= element.scrollTop || 0;
valueL -= element.scrollLeft || 0;
}
} while (element = element.parentNode);

return Element._returnOffset(valueL, valueT);
};
Element._returnOffset = function(l, t) {
var result = [l, t];
result.left = l;
result.top = t;
return result;
};

var Event;
if(!Event){
Event = {};
}
Event.stop = function(e){
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
if(e.preventDefault){
e.preventDefault();
}else{
e.returnValue = false;
}
}
Event.addListener = function(target, eventType, handler, flag){
if(document.attachEvent){
target.attachEvent("on"+eventType, handler);
}else if(document.addEventListener){
target.addEventListener(eventType, handler, flag);
}else{
target["on"+eventType] = handler;
}
}
Event.removeListener = function(target, eventType, handler, flag){
if(document.detachEvent){
target.detachEvent("on"+eventType, handler);
}else if(document.removeEventListener){
target.removeEventListener(eventType, handler, flag);
}else{
target["on"+eventType] = null;
}
}
Event.target = function(el){
if(el.srcElement){
return el.srcElement;
}else{
return el.target;
}
}
function initLocation(){
var p = Element.viewportOffset(_target);
var pp = document.viewport.getScrollOffsets();

calendar.style.left = p.left+pp.left+"px";
var height = _target.clientHeight;
calendar.style.top = p.top +pp.top+ height+5 +"px";

}
calendar.style.top = p.top +pp.top+ height+5 +"px";还显示参数无效
调用时显示'null' 为空或不是对象
<script type="text/javascript" language="javascript" src="../../Styles/calendar.js"></script>
<script>
window.onload = function () {
c1 = new JSCalendar(document.getElementById("transdate1"));
// c2 = new JSCalendar(document.getElementById("transdate2"));
function format(date) {
return date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
}

var dateEl = document.getElementById("transdate1")
dateEl.onDateChange = function (oldDate, newDate) {

}
// var dateEl1 = document.getElementById("transdate2")
// dateEl.onDateChange = function (oldDate, newDate) {

// }
}
<asp:TextBox ID="transdate1" runat="server"></asp:TextBox>

var myTextbox=document.getElementById("myRegisterTime");
改为:
var myTextbox=document.getElementById("<%=myRegisterTime.ClientID%>");
导致这个问题的出现是当你引用母版页时,TEXTBOX的实际ID在客户端会发生变化

另外,像你这种需求可以使用Jquery的datepicker插件,实现起来比你这种方式简单得多,见参考资料.
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-05-11
如果用了母版页的话,就要取ClientID,代码太长,本人没时间细看!不然,追问就好了~
第2个回答  2011-05-11
你用的是calendar日历控件的话应该引用WdatePicker.js文件而不是calendar.js
第3个回答  2011-05-11
<asp:TextBox ID="transdate1" runat="server"></asp:TextBox>都没有调用js怎么能实现啊?追问

var dateEl = document.getElementById("transdate1")通过这个来获取的不对吗?

追答

你不是想点击textbox显示出来日立控件吗?但是你textbox都没有调用js怎么能实现那?在textbox中添加点击事件,让他调用js,来实现显示日历,

相似回答
大家正在搜