在PHP中构造的JavaScript onClick函数在ajax响应中不起作用

时间:2014-07-05 05:11:37

标签: javascript php jquery ajax

我发送一个ajax请求来加载PHP中div构造的一些信息。那里有一个onClick功能。 Ajax响应很好,但JavaScript函数在HTML客户端中不起作用。

这是用PHP编写的ajax的响应:

    $iscId = $row['instructorSemesterCourseId'];
    $year = $row['year'];
    $startDate = $row['startDate'];
    $endDate = $row['endDate'];
    $courseCredit = $row['courseCredit'];
    $pinned = $row['pinned'];
    $semesterName = $row['semesterName'];

    $date1 = date('F j, Y', strtotime($startDate)); 
    $date2 = date('F j, Y', strtotime($endDate));

    $div .= "<div id='SCid".$iscId."' class='schoolClass'>
                  <div class='schoolClassLeft'>
                    <div class='schoolCourseTitle'  onClick='classSessionEnter('SCid".$iscId."'); style='width:100%'>".$semesterName." ".$year." | ".$date1." &ndash; ".$date2."</div>
                  </div>
                </div>";
    echo('$div');

用JavaScript和jQuery编写的函数是:

function classSessionEnter(elm) {
    alert('elm');
    $('#' + elm).css({"background": "#E7F9CE"});
    $('#' + elm).css({"position": "absolute"});
    $('#' + elm).css({"top": "0px"});
    $('#' + elm).css({"color": "#006600"});
}

警报不起作用 - 它根本不进入该功能。为什么呢?

2 个答案:

答案 0 :(得分:2)

替换此

$div .= "<div id='SCid".$iscId."' class='schoolClass'>
              <div class='schoolClassLeft'>
                <div class='schoolCourseTitle'  onClick='classSessionEnter('SCid".$iscId."'); style='width:100%'>".$semesterName." ".$year." | ".$date1." &ndash; ".$date2."</div>
              </div>
            </div>";
echo('$div');

用这个

$div .= "<div id='SCid".$iscId."' class='schoolClass'>
                  <div class='schoolClassLeft'>
                    <div class='schoolCourseTitle'  onClick='classSessionEnter(SCid".$iscId.");' style='width:100%'>".$semesterName." ".$year." | ".$date1." &ndash; ".$date2."</div>
                  </div>
                </div>";
    echo($div);

答案 1 :(得分:1)

$div .= "<div id='SCid".$iscId."' class='schoolClass'>
    <div class='schoolClassLeft'>
        <div class='schoolCourseTitle' onClick=\"classSessionEnter('SCid".$iscId."')\"; style='width:100%'>".$semesterName." ".$year." | ".$date1." &ndash; ".$date2."</div>
     </div>
</div>";

你应该首先逃避引号“onClick = \&#34;”