简体   繁体   中英

jQuery .click on TD not firing

So I'm just trying to get a message box to pop up when I click a cell in my table. I have seen many threads about $("td").click(function () { etc, but these are not working for my table. I cannot find out why.

HTML:

<body>
<div id="tableArea">
    <table class="table table-bordered" id="myTable">
        <tr>
            <td bgcolor="green" id='a1'>1</td><td>2</td>
        </tr>
    </table>
</div>
</body>

And the javascript/jQuery:

<script type="text/javascript">
    $("td").click(function(e) {
        alert('Anything');
    });
</script>

I do not see a difference in this code than in many of the other threads, but this is not working. Note: I'm using Bootstrap, if that makes a difference.

You should capture the click event after $(document).ready().

<script type="text/javascript">
    $(document).ready(function() {
        $("td").click(function(e) {
            alert('Anything');
        });
    });
</script>
$(document).ready( function(){
    $("td").on('click', function(e) {
        alert('Anything');
    });
});

just work fine for your markup.

see it on jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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