关于下面的回答,我的浏览器控制台出现以下错误
根据浏览器控制台错误在这一行
未捕获的类型错误:无法设置 nullcal.php:26(匿名函数)的属性“onclick”
当单击除 1 月、4 月、11 月以外的日期时,表单不会打开。
like 26 上的代码来自控制台浏览器是
document.getElementById('trigger0').onclick = function()
{showForm()};document.getElementById('trigger1').onclick = function()
{showForm()};document.getElementById('trigger2').onclick = function()
{showForm()};document.getElementById('trigger3').onclick = function()
{showForm()};document.getElementById('trigger4').onclick = function()
{showForm()};document.getElementById('trigger5').onclick = function()
{showForm()};document.getElementById('trigger6').onclick = function()
{showForm()};document.getElementById('trigger7').onclick = function()
{showForm()};document.getElementById('trigger8').onclick = function()
{showForm()};document.getElementById('trigger9').onclick = function()
{showForm()};document.getElementById('trigger10').onclick = function()
{showForm()};document.getElementById('trigger11').onclick = function()
{showForm()};document.getElementById('trigger12').onclick = function()
{showForm()};document.getElementById('trigger13').onclick = function()
{showForm()};document.getElementById('trigger14').onclick = function()
{showForm()};document.getElementById('trigger15').onclick = function()
{showForm()};document.getElementById('trigger16').onclick = function()
{showForm()};document.getElementById('trigger17').onclick = function()
{showForm()};document.getElementById('trigger18').onclick = function()
{showForm()};document.getElementById('trigger19').onclick = function()
{showForm()};document.getElementById('trigger20').onclick = function()
{showForm()};document.getElementById('trigger21').onclick = function()
{showForm()};document.getElementById('trigger22').onclick = function()
{showForm()};document.getElementById('trigger23').onclick = function()
{showForm()};document.getElementById('trigger24').onclick = function()
{showForm()};document.getElementById('trigger25').onclick = function()
{showForm()};document.getElementById('trigger26').onclick = function()
{showForm()};document.getElementById('trigger27').onclick = function()
{showForm()};document.getElementById('trigger28').onclick = function()
{showForm()};document.getElementById('trigger29').onclick = function()
{showForm()};document.getElementById('trigger30').onclick = function()
{showForm()};document.getElementById('trigger31').onclick = function()
{showForm()};document.getElementById('trigger32').onclick = function()
{showForm()};document.getElementById('trigger33').onclick = function()
{showForm()};document.getElementById('trigger34').onclick = function()
{showForm()}; function showForm(){
它涉及的代码是:
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
$today = getdate();
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) echo "<tr> ";
if($i < $startday) echo "<td></td> ";
else echo "<td align='center' valign='middle' height='20px'><a href='#'
id='trigger" . $i . "'>". ($i - $startday + 1) . "</a></td>";
$jsEvent[] = "document.getElementById('trigger" . $i . "').onclick =
function() {showForm()};";
if(($i % 7) == 6 ) echo "</tr> ";
}
?>
javascript代码
<script type="text/javascript">
<?php foreach($jsEvent as $event){
echo $event;
} ?>
function showForm(){
document.getElementById('timeslots').style.display="block";
};
</script>
最佳答案
您可以为此使用 javascript。您可以使用 onClick=""来触发隐藏/显示表单的 js 函数。
<script type="text/javascript">
<?php foreach($jsEvent as $event){
echo $event;
} ?>
function showForm(){
document.getElementById('timeslots').style.display="block";
};
</script>
然后改变
<table width='400' border='2' cellpadding='2' cellspacing='2' id='timeslots'
style="visibility:invisible"'>
到
<table width='400' border='2' cellpadding='2' cellspacing='2' id='timeslots'
style="display:none;"'>
同时改变这个:
<td align='center' valign='middle' height='20px'><a href='#'>". ($i - $startday + 1) . "</a></td>
为此:
<td align='center' valign='middle' height='20px'><a href='#' id='trigger" . $i . "'>". ($i - $startday + 1) . "</a></td>
请记住,“editform”只是一个建议的 ID,需要与您使用的触发器/日期的 ID 相匹配。
我已经尝试并搜索了高低,但我无法提出另一种解决方案,用下面的代码替换你的 for 循环(for 和其中的所有内容)
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ){
echo "<tr> ";
}
if($i < $startday){
echo "<td></td> ";
} else{
$jsEvent[] = "document.getElementById('trigger" . $i . "').onclick =
function() {showForm()};" . PHP_EOL;
echo "<td align='center' valign='middle' height='20px'><a href='#'
id='trigger" . $i . "'>". ($i - $startday + 1) . "</a></td>";
}
if(($i % 7) == 6 ){
echo "</tr> ";
}
}
JS 函数可以更高级,比如检查或隐藏其他打开的表单,这只是一个简单的 JS 函数,通过 ID 显示单个表单
更新 根据评论中的信息更新了我的答案:),现在您只需要更改第一个文档的 ID。GetelemtentByID 以匹配您要触发该功能的“链接/文本”的 ID。
关于javascript - 浏览器控制台错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28046260/