关于下面的回答,我的浏览器控制台中有以下错误
根据浏览器控制台,错误在这一行
Uncaught TypeError:无法设置nullcal的属性“onclick”。php:26(匿名函数)
当单击a日期而不是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显示单个表单
更新
根据comments:)中的信息更新了我的答案,现在只需更改第一个document.GetelemtentByID的ID,以匹配要触发函数的“link/text”的ID。

07-24 18:59
查看更多