【发布时间】:2019-04-19 01:58:03
【问题描述】:
我有这个动态创建的教室,侧面有 3 个按钮。
for classroom in classrooms
if classroom.author.id == user.id
tr#bothcells
td#classroomcell= classroom.classroomname
td#buttonscell
button.renameclassroom(data-toggle="tooltip", data-placement="top" ,title="Rename this classroom" type='button' style='font-size:15px', onclick='renameclassroom($(this))' value=classroom.id)
i.fas.fa-pencil-alt
button.showstudents(data-toggle="tooltip", data-placement="top" ,title="Show all students belonging to this classroom" style='font-size:15px' type='button', onclick='get($(this))', value=classroom.id )
i.fas.fa-user-graduate
a.deleteclassroom(data-toggle="tooltip", data-placement="top" ,title="Delete this classroom" style='font-size:15px',href="/classroom/delete/" + classroom._id)
i.fas.fa-times
..这个 jQuery AJAX 用于获取属于该教室的学生的姓名。
function get(a){
var classroomvalue = a.val();
$.ajax({
type: 'GET',
url: 'http://localhost:8080/classroom/' + classroomvalue,
})
.done(function(data) {
console.log('Get response:', JSON.stringify(data, "", 2));
$("#getResponse").html($(data).find('#students').html());
})
.fail(function(jqXHR, textStatus, err) {
console.log('Ajax error response:', textStatus);
});
};
我在哪里挣扎,我的问题是什么? 我需要在按下时更改按钮图标颜色,因为教室和按钮是动态创建的,我知道我不能使用 Id 但值。我很难找到解决方案,而且我想不出任何办法。
【问题讨论】:
-
为什么你的问题标题提到了
div?你是什么意思,我知道我不能使用 Id 但值?您提到更改按钮icon 颜色。图标集在哪里?一个图标通常是一个图形,所以要改变它的颜色,你可能只需要改变图标。 -
抱歉,我需要更改 div 背景,但我意识到如何更容易解释如何更改图标的颜色,因为我可以得到这样做的提示,我将能够也做 div。
-
您显示的代码没有
div。更改div的背景很容易。谷歌搜索“jQuery 更改 div 背景”。 -
@lurker,我不确定你是否理解我的问题,如果我解释得不够好,我深表歉意。我需要在按下时更改颜色,如 CSS 中的 :active 。但它们是动态创建的。
-
我可能没听懂。我没有在您的问题中看到任何动态创建任何元素的代码您不能使用唯一的 Id 动态创建它们(您可以通过各种方式构建 id)然后创建点击操作功能吗?您还可以对匹配模式的 id 创建操作。