简体   繁体   English

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

[英]JavaScript onClick function constructed in PHP does not work in the ajax response back

I am sending an ajax request to load some information constructed in a div from PHP. 我正在发送ajax请求以从PHP加载在div中构造的一些信息。 There is an onClick function there. 那里有一个onClick函数。 Ajax responds well, but the JavaScript function does not work in the HTML client. Ajax的响应很好,但是JavaScript功能在HTML客户端中不起作用。

This is the response from ajax written in PHP: 这是用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');

The function written in JavaScript and jQuery is: 用JavaScript和jQuery编写的函数是:

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

The alert does not work - it does not get into the function at all. 警报不起作用-根本不进入功能。 Why? 为什么?

Replace this 取代这个

$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');

with this 有了这个

$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>";

You should esacpe the quotes first 「onClick=\\"」 您应该先保留引号「onClick = \\“」

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM