我正在使用scw日期选择器库。我使用了很长时间的Javascript代码现在无法正常工作。 onclick
的输入功能在第一行有效,但在第二行和其他行无效。
我使用javascript代码动态创建了输入。
JavaScript代码
function AddRow(tbody_id, satir_id)
{
var tbody = document.getElementById(tbody_id);
var row = document.createElement("tr");
row.id = "s" + satir_id;
var input0 = document.createElement("input");
input0.id = row.id + "_i0";
input0.name = "satir[" + satir_id+ "][0]";
input0.className = "text";
input0.size = "11";
input0.onclick = function onclick()
{
scwNextAction = tmp.runAfterSCW(this);
scwShow(this, event);
}
}
此input0.onclick
方法在第一行工作。但这在第二行和另一行上不起作用。
错误如下:
input0未定义 堆栈:“ ReferenceError:在eval上未定义input0”(在HTMLDocument.positiontip(http://portaltest.gural.tr/phpgroupware/kys/siparis/ortak/kutuphane/form.js?v2.0.1035:1:1)上的eval(位置提示(http://portaltest.gural.tr/phpgroupware/kys/siparis/ortak/kutuphane/form.js?v2.0.1035:334:21)上的:1:1)↵:“
相同的代码在Internet Explorer上有效,但在Google Crome上无效。两者(互联网浏览器,谷歌浏览器)都存在参考错误。
您认为我该如何解决这种情况?
答案 0 :(得分:0)
您可以尝试添加eventListener而不使用onclick属性
function AddRow(tbody_id, satir_id)
{
var tbody = document.getElementById(tbody_id);
var row = document.createElement("tr");
row.id = "s" + satir_id;
var input0 = document.createElement("input");
input0.id = row.id + "_i0";
input0.name = "satir[" + satir_id+ "][0]";
input0.className = "text";
input0.size = "11";
input0.addEventlistener("click", function(evt) {
scwNextAction = tmp.runAfterSCW(this);
scwShow(this, evt);
})
}
否则,您在input0
或tmp.runAfterSCW()
中引用了scwShow()
吗?
答案 1 :(得分:0)
我有用于scw日期选择器库的scw.js。
对于runsAfterSCW函数:
Function.prototype.runsAfterSCW =
function(){var func = this,
args = new Array(arguments.length);
for (var i=0;i<args.length;++i) {args[i] = arguments[i];}
return function()
{// concat/join the two argument arrays
for (var i=0;i<arguments.length;++i) {args[args.length] = arguments[i];}
return (args.shift()==scwTriggerEle)?func.apply(this, args):null;
};
};
For scwShow function:
function scwShow(scwEle,scwSource)
{if (!scwSource) {scwSource = window.event;}
if (scwSource.tagName) // Second parameter isn't an event it's an element
{var scwSourceEle = scwSource;
if (scwID('scwIE')) {window.event.cancelBubble = true;}
else {scwSourceEle.parentNode.addEventListener('click',scwStopPropagation,false);}
}
else // Second parameter is an event
{var scwSourceEle = (scwSource.target)
?scwSource.target
:scwSource.srcElement;
// Stop the click event that opens the calendar from bubbling up to
// the document-level event handler that hides it!
if (scwSource.stopPropagation) {scwSource.stopPropagation();}
else {scwSource.cancelBubble = true;}
}
scwTriggerEle = scwSourceEle;
// Take any parameters that there might be from the third onwards as
// day numbers to be disabled 0 = Sunday through to 6 = Saturday.
scwParmActiveToday = true;
for (var i=0;i<7;i++)
{scwPassEnabledDay[(i+7-scwWeekStart)%7] = true;
for (var j=2;j<arguments.length;j++)
{if (arguments[j]==i)
{scwPassEnabledDay[(i+7-scwWeekStart)%7] = false;
if (scwDateNow.getDay()==i) {scwParmActiveToday = false;}
}
}
}
// If no value is preset then the seed date is
// Today (when today is in range) OR
// The middle of the date range.
scwSeedDate = scwDateNow;
// Find the date and Strip space characters from start and
// end of date input.
var scwDateValue = '';
if (scwEle.value) {scwDateValue = scwEle.value.replace(/^\s+/,'').replace(/\s+$/,'');}
else {if (typeof scwEle.value == 'undefined')
{var scwChildNodes = scwEle.childNodes;
for (var i=0;i<scwChildNodes.length;i++)
{if (scwChildNodes[i].nodeType == 3)
{scwDateValue = scwChildNodes[i].nodeValue.replace(/^\s+/,'').replace(/\s+$/,'');
if (scwDateValue.length > 0)
{scwTriggerEle.scwTextNode = scwChildNodes[i];
scwTriggerEle.scwLength = scwChildNodes[i].nodeValue.length;
break;
}
}
}
}
}
// Set the language-dependent elements
scwSetDefaultLanguage();
scwID('scwDragText').innerHTML = scwDrag;
scwID('scwMonths').options.length = 0;
for (var i=0;i<scwArrMonthNames.length;i++)
{scwID('scwMonths').options[i] = new Option(scwArrMonthNames[i],scwArrMonthNames[i]);}
scwID('scwYears').options.length = 0;
for (var i=0;i<scwDropDownYears;i++)
{scwID('scwYears').options[i] = new Option((scwBaseYear+i),(scwBaseYear+i));}
for (var i=0;i<scwArrWeekInits.length;i++)
{scwID('scwWeekInit' + i).innerHTML = scwArrWeekInits[(i+scwWeekStart)%scwArrWeekInits.length];}
if (((new Date(scwBaseYear + scwDropDownYears, 0, 0)) > scwDateNow &&
(new Date(scwBaseYear, 0, 0)) < scwDateNow) ||
(scwClearButton && (scwEle.readOnly || scwEle.disabled))
) {scwID('scwFoot').style.display = '';
scwID('scwNow').innerHTML = scwToday + ' ' + scwDateNow.scwFormat(scwDateDisplayFormat);
scwID('scwClearButton').value = scwClear;
if ((new Date(scwBaseYear + scwDropDownYears, 0, 0)) > scwDateNow &&
(new Date(scwBaseYear, 0, 0)) < scwDateNow
) {scwID('scwNow').style.display = '';
if (scwClearButton && (scwEle.readOnly || scwEle.disabled))
{scwID('scwClear').style.display = '';
scwID('scwClear').style.textAlign = 'left';
scwID('scwNow').style.textAlign = 'right';
}
else {scwID('scwClear').style.display = 'none';
scwID('scwNow').style.textAlign = 'center';
}
}
else {scwID('scwClear').style.textAlign = 'center';
scwID('scwClear').style.display = '';
scwID('scwNow').style.display = 'none';
}
}
else {scwID('scwFoot').style.display = 'none';}
if (scwDateValue.length==0)
{// If no value is entered and today is within the range,
// use today's date, otherwise use the middle of the valid range.
scwBlnFullInputDate=false;
if ((new Date(scwBaseYear+scwDropDownYears,0,0))<scwSeedDate ||
(new Date(scwBaseYear,0,1)) >scwSeedDate
)
{scwSeedDate = new Date(scwBaseYear + Math.floor(scwDropDownYears / 2), 5, 1);}
}
else
{function scwInputFormat()
{var scwArrSeed = new Array(),
scwArrInput = scwDateValue.split(new RegExp('[\\'+scwArrDelimiters.join('\\')+']+','g'));
// "Escape" all the user defined date delimiters above -
// several delimiters will need it and it does no harm for
// the others.
// Strip any empty array elements (caused by delimiters)
// from the beginning or end of the array. They will
// still appear in the output string if in the output
// format.
if (scwArrInput[0]!=null)
{if (scwArrInput[0].length==0) {scwArrInput.splice(0,1);}
if (scwArrInput[scwArrInput.length-1].length==0) {scwArrInput.splice(scwArrInput.length-1,1);}
}
scwBlnFullInputDate = false;
scwDateOutputFormat = scwDateOutputFormat.toUpperCase();
// List all the allowed letters in the date format
var template = ['D','M','Y'];
// Prepare the sequence of date input elements
var result = new Array();
for (var i=0;i<template.length;i++)
{if (scwDateOutputFormat.search(template[i])>-1)
{result[scwDateOutputFormat.search(template[i])] = template[i];}
}
var scwDateSequence = result.join('');
// Separate the elements of the date input
switch (scwArrInput.length)
{case 1:
{if (scwDateOutputFormat.indexOf('Y')>-1 &&
scwArrInput[0].length>scwDateOutputFormat.lastIndexOf('Y'))
{scwArrSeed[0] = parseInt(scwArrInput[0].substring(scwDateOutputFormat.indexOf('Y'),
scwDateOutputFormat.lastIndexOf('Y')+1),10);
}
else {scwArrSeed[0] = 0;}
if (scwDateOutputFormat.indexOf('M')>-1 &&
scwArrInput[0].length>scwDateOutputFormat.lastIndexOf('M'))
{scwArrSeed[1] = scwArrInput[0].substring(scwDateOutputFormat.indexOf('M'),
scwDateOutputFormat.lastIndexOf('M')+1);
}
else {scwArrSeed[1] = '6';}
if (scwDateOutputFormat.indexOf('D')>-1 &&
scwArrInput[0].length>scwDateOutputFormat.lastIndexOf('D'))
{scwArrSeed[2] = parseInt(scwArrInput[0].substring(scwDateOutputFormat.indexOf('D'),
scwDateOutputFormat.lastIndexOf('D')+1),10);
}
else {scwArrSeed[2] = 1;}
if (scwArrInput[0].length==scwDateOutputFormat.length) {scwBlnFullInputDate = true;}
break;
}
case 2:
{// Year and Month entry
scwArrSeed[0] =
parseInt(scwArrInput[scwDateSequence.
replace(/D/i,'').
search(/Y/i)],10); // Year
scwArrSeed[1] = scwArrInput[scwDateSequence.
replace(/D/i,'').
search(/M/i)]; // Month
scwArrSeed[2] = 1; // Day
break;
}
case 3:
{// Day Month and Year entry
scwArrSeed[0] =
parseInt(scwArrInput[scwDateSequence.
search(/Y/i)],10); // Year
scwArrSeed[1] = scwArrInput[scwDateSequence.
search(/M/i)]; // Month
scwArrSeed[2] =
parseInt(scwArrInput[scwDateSequence.
search(/D/i)],10); // Day
scwBlnFullInputDate = true;
break;
}
default:
{// A stuff-up has led to more than three elements in
// the date.
scwArrSeed[0] = 0; // Year
scwArrSeed[1] = 0; // Month
scwArrSeed[2] = 0; // Day
}
}
// These regular expressions validate the input date format
// to the following rules;
// Day 1-31 (optional zero on single digits)
// Month 1-12 (optional zero on single digits)
// or case insensitive name
// Year One, Two or four digits
// Months names are as set in the language-dependent
// definitions and delimiters are set just below there
var scwExpValDay = new RegExp('^(0?[1-9]|[1-2][0-9]|3[0-1])$'),
scwExpValMonth = new RegExp('^(0?[1-9]|1[0-2]|' +
scwArrMonthNames.join('|') +
')$','i'),
scwExpValYear = new RegExp('^([0-9]{1,2}|[0-9]{4})$');
// Apply validation and report failures
if (scwExpValYear.exec(scwArrSeed[0]) == null ||
scwExpValMonth.exec(scwArrSeed[1]) == null ||
scwExpValDay.exec(scwArrSeed[2]) == null
)
{if (scwShowInvalidDateMsg)
{alert(scwInvalidDateMsg +
scwInvalidAlert[0] + scwDateValue +
scwInvalidAlert[1]);}
scwBlnFullInputDate = false;
scwArrSeed[0] = scwBaseYear +
Math.floor(scwDropDownYears/2); // Year
scwArrSeed[1] = '6'; // Month
scwArrSeed[2] = 1; // Day
}
// Return the Year in scwArrSeed[0]
// Month in scwArrSeed[1]
// Day in scwArrSeed[2]
return scwArrSeed;
};
// Parse the string into an array using the allowed delimiters
scwArrSeedDate = scwInputFormat();
// So now we have the Year, Month and Day in an array.
// If the year is one or two digits then the routine assumes a
// year belongs in the 21st Century unless it is less than 50
// in which case it assumes the 20th Century is intended.
if (scwArrSeedDate[0]<100) {scwArrSeedDate[0] += (scwArrSeedDate[0]>50)?1900:2000;}
// Check whether the month is in digits or an abbreviation
if (scwArrSeedDate[1].search(/\d+/)<0)
{for (i=0;i<scwArrMonthNames.length;i++)
{if (scwArrSeedDate[1].toUpperCase()==scwArrMonthNames[i].toUpperCase())
{scwArrSeedDate[1]=i+1;
break;
}
}
}
scwSeedDate = new Date(scwArrSeedDate[0],scwArrSeedDate[1]-1,scwArrSeedDate[2]);
}
// Test that we have arrived at a valid date
if (isNaN(scwSeedDate))
{if (scwShowInvalidDateMsg) {alert(scwInvalidDateMsg + scwInvalidAlert[0] + scwDateValue + scwInvalidAlert[1]);}
scwSeedDate = new Date(scwBaseYear + Math.floor(scwDropDownYears/2),5,1);
scwBlnFullInputDate=false;
}
else
{// Test that the date is within range,
// if not then set date to a sensible date in range.
if ((new Date(scwBaseYear,0,1)) > scwSeedDate)
{if (scwBlnStrict && scwShowOutOfRangeMsg) {alert(scwOutOfRangeMsg);}
scwSeedDate = new Date(scwBaseYear,0,1);
scwBlnFullInputDate=false;
}
else
{if ((new Date(scwBaseYear+scwDropDownYears,0,0))<scwSeedDate)
{if (scwBlnStrict && scwShowOutOfRangeMsg) {alert(scwOutOfRangeMsg);}
scwSeedDate = new Date(scwBaseYear + Math.floor(scwDropDownYears)-1,11,1);
scwBlnFullInputDate=false;
}
else
{if (scwBlnStrict && scwBlnFullInputDate &&
(scwSeedDate.getDate() != scwArrSeedDate[2] ||
(scwSeedDate.getMonth()+1) != scwArrSeedDate[1] ||
scwSeedDate.getFullYear() != scwArrSeedDate[0]
)
)
{if (scwShowDoesNotExistMsg) alert(scwDoesNotExistMsg);
scwSeedDate = new Date(scwSeedDate.getFullYear(),scwSeedDate.getMonth()-1,1);
scwBlnFullInputDate=false;
}
}
}
}
// Test the disabled dates for validity
// Give error message if not valid.
for (var i=0;i<scwDisabledDates.length;i++)
{if (!((typeof scwDisabledDates[i] == 'object') && (scwDisabledDates[i].constructor == Date)))
{if ((typeof scwDisabledDates[i] == 'object') && (scwDisabledDates[i].constructor == Array))
{var scwPass = true;
if (scwDisabledDates[i].length !=2)
{if (scwShowRangeDisablingError)
{alert(scwRangeDisablingError[0] + scwDisabledDates[i] + scwRangeDisablingError[1]);}
scwPass = false;
}
else
{for (var j=0;j<scwDisabledDates[i].length;j++)
{if (!((typeof scwDisabledDates[i][j] == 'object') && (scwDisabledDates[i][j].constructor == Date)))
{if (scwShowRangeDisablingError)
{alert( scwDateDisablingError[0] + scwDisabledDates[i][j] + scwDateDisablingError[1]);}
scwPass = false;
}
}
}
if (scwPass && (scwDisabledDates[i][0] > scwDisabledDates[i][1])) {scwDisabledDates[i].reverse();}
}
else
{if (scwShowRangeDisablingError) {alert(scwDateDisablingError[0] + scwDisabledDates[i] + scwDateDisablingError[1]);}}
}
}
// Calculate the number of months that the entered (or
// defaulted) month is after the start of the allowed
// date range.
scwMonthSum = 12*(scwSeedDate.getFullYear()-scwBaseYear)+scwSeedDate.getMonth();
scwID('scwYears' ).options.selectedIndex = Math.floor(scwMonthSum/12);
scwID('scwMonths').options.selectedIndex = (scwMonthSum%12);
// Check whether or not dragging is allowed and display drag handle if necessary
scwID('scwDrag').style.display=(scwAllowDrag)?'':'none';
// Display the month
scwShowMonth(0);
// Position the calendar box
// The object sniffing for Opera allows for the fact that Opera
// is the only major browser that correctly reports the position
// of an element in a scrollable DIV. This is because IE and
// Firefox omit the DIV from the offsetParent tree.
scwTargetEle=scwEle;
var offsetTop =parseInt(scwEle.offsetTop ,10) + parseInt(scwEle.offsetHeight,10),
offsetLeft=parseInt(scwEle.offsetLeft,10);
if (!window.opera)
{while (scwEle.tagName!='BODY' && scwEle.tagName!='HTML')
{offsetTop -=parseInt(scwEle.scrollTop, 10);
offsetLeft-=parseInt(scwEle.scrollLeft,10);
scwEle=scwEle.parentNode;
}
scwEle=scwTargetEle;
}
do {scwEle=scwEle.offsetParent;
offsetTop +=parseInt(scwEle.offsetTop, 10);
offsetLeft+=parseInt(scwEle.offsetLeft,10);
}
while (scwEle.tagName!='BODY' && scwEle.tagName!='HTML');
if (scwAutoPosition)
{var scwWidth = parseInt(scwID('scw').offsetWidth, 10),
scwHeight = parseInt(scwID('scw').offsetHeight,10),
scwWindowLeft =
(document.body && document.body.scrollLeft)
?document.body.scrollLeft //DOM compliant
:(document.documentElement && document.documentElement.scrollLeft)
?document.documentElement.scrollLeft //IE6+ standards compliant
:0, //Failed
scwWindowWidth =
(typeof(innerWidth) == 'number')
?innerWidth //DOM compliant
:(document.documentElement && document.documentElement.clientWidth)
?document.documentElement.clientWidth //IE6+ standards compliant
:(document.body && document.body.clientWidth)
?document.body.clientWidth //IE non-compliant
:0, //Failed
scwWindowTop =
(document.body && document.body.scrollTop)
?document.body.scrollTop //DOM compliant
:(document.documentElement && document.documentElement.scrollTop)
?document.documentElement.scrollTop //IE6+ standards compliant
:0, //Failed
scwWindowHeight =
(typeof(innerHeight) == 'number')
?innerHeight //DOM compliant
:(document.documentElement && document.documentElement.clientHeight)
?document.documentElement.clientHeight //IE6+ standards compliant
:(document.body && document.body.clientHeight)
?document.body.clientHeight //IE non-compliant
:0; //Failed
offsetLeft -= (offsetLeft - scwWidth + parseInt(scwTargetEle.offsetWidth,10) >= scwWindowLeft &&
offsetLeft + scwWidth > scwWindowLeft + scwWindowWidth
)?(scwWidth - parseInt(scwTargetEle.offsetWidth,10)):0;
offsetTop -= (offsetTop - scwHeight - parseInt(scwTargetEle.offsetHeight,10) >= scwWindowTop &&
offsetTop + scwHeight > scwWindowTop + scwWindowHeight
)?(scwHeight + parseInt(scwTargetEle.offsetHeight,10)):0;
}
scwID('scw').style.top = offsetTop+'px';
scwID('scw').style.left = offsetLeft+'px';
scwID('scwIframe').style.top = offsetTop+'px';
scwID('scwIframe').style.left = offsetLeft+'px';
scwID('scwIframe').style.width =(scwID('scw').offsetWidth-(scwID('scwIE')?2:4))+'px';
scwID('scwIframe').style.height=(scwID('scw').offsetHeight-(scwID('scwIE')?2:4))+'px';
scwID('scwIframe').style.visibility='inherit';
// Show it on the page
scwID('scw').style.visibility='inherit';
};