慕森王
2021-12-12 16:08:31
我正在嘗試使用 JQuery 在單擊時切換 html 表行的類“突出顯示”。該表是使用 Django 模板語言創(chuàng)建的。該表可以工作并顯示在我的開發(fā)服務(wù)器中,而 Jquery 可以處理不是用 Django 模板語言創(chuàng)建的表。運(yùn)行代碼時我沒有收到任何錯誤,但是當(dāng)我單擊表格行時它沒有做任何事情。我不確定問題可能是什么。HTML<style media="screen"> .highlight { background-color: yellow; color: white; }</style><div class="table-responsive"> <table class="table table table-borderless" id="food_table"> <thead> <tr> <th>#</th> <th>Description</th> <th>Price</th> </tr> </thead> <tbody> {% for order in orders %} <tr> <td>{{ order.pk }}</td> <td>{{ order.Price }}</td> <td>{{ order.Description }}</td> </tr> {% endfor %} </tbody> </table></div>查詢$("#food_table tbody").on('click','tr',function() { $(this).toggleClass("highlight"); });虛擬數(shù)據(jù)[ { "pk": 9, "Description": "Pizza", "Price": "88.00" }, { "pk": 10, "Description": "Steak", "Price": "130.50" }, { "pk": 11, "Description": "Coca Cola", "Price": "25.95" }, { "pk": 12, "Description": "Water", "Price": "15.00" }]
3 回答

翻翻過去那場雪
TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個贊
嘗試將其從 body
$("body").on('click','#food_table tbody tr',function() {
$(this).toggleClass("highlight");
});

慕村9548890
TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個贊
你必須嘗試一些這樣的。#food_table 必須使用 .click 函數(shù) jquery 調(diào)用,并且必須在 html 模板中導(dǎo)入 jquery 資源
$("#food_table").click(function(){
$(this).toggleClass('highlight');
});
這對我有用。

qq_花開花謝_0
TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個贊
嘗試從身體上做
$('body').on('click', '#food_table tbody tr', function(){
$(this).toggleClass('highlight');
});
添加回答
舉報(bào)
0/150
提交
取消