为什么点击<tr>在这种情况下不起作用

Why onclick of <tr> is not working in this case?

本文关键字:这种情况下 不起作用 tr 为什么      更新时间:2023-09-26

奇怪的是,当我把<tr>的onclick放在JSFiddle上时,它在我的情况下不起作用http://jsfiddle.net/dad5m5vb/

这是HTML:

<form id="form_city" action="">
    <table width="100%" border="1" cellspacing="0" cellpadding="0">
        <tr>
            <th>City Id</th>
            <th>City Name</th>
        </tr>
        <tr id="row1" onclick="rowClicked(this);">
            <td>1</td>
            <td>New York</td>
        </tr>
        <tr id="row2" onclick="rowClicked(this);">
            <td>2</td>
            <td>Los Angeles</td>
        </tr>
        <tr id="row2" onclick="rowClicked(this);">
            <td>3</td>
            <td>Chicago</td>
        </tr>
    </table>
</form>

这是JavaScript:

function rowClicked(clickedRow) {
    alert(clickedRow);
}

点击任何一行(除了标题),javascript函数应该被调用。但我没有得到警报从javascript函数。为什么?

您必须将JSFiddle onLoad更改为no wrap - in <head> (http://prntscr.com/77j08q),以便代码在<head>中加载,而不是在DOM加载时加载