【问题标题】:How to change the color of a dynamically created element?如何更改动态创建的元素的颜色?
【发布时间】: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 但值。我很难找到解决方案,而且我想不出任何办法。

已生成更新的 HTML

【问题讨论】:

  • 为什么你的问题标题提到了div?你是什​​么意思,我知道我不能使用 Id 但值?您提到更改按钮icon 颜色。图标集在哪里?一个图标通常是一个图形,所以要改变它的颜色,你可能只需要改变图标。
  • 抱歉,我需要更改 div 背景,但我意识到如何更容易解释如何更改图标的颜色,因为我可以得到这样做的提示,我将能够也做 div。
  • 您显示的代码没有div。更改div 的背景很容易。谷歌搜索“jQuery 更改 div 背景”。
  • @lurker,我不确定你是否理解我的问题,如果我解释得不够好,我深表歉意。我需要在按下时更改颜色,如 CSS 中的 :active 。但它们是动态创建的。
  • 我可能没听懂。我没有在您的问题中看到任何动态创建任何元素的代码您不能使用唯一的 Id 动态创建它们(您可以通过各种方式构建 id)然后创建点击操作功能吗?您还可以对匹配模式的 id 创建操作。

标签: jquery html pug


【解决方案1】:

首先,让多个元素共享同一个 id 是错误的,因为 id 属性旨在唯一标识一个元素。除此之外,在这种情况下,您可以使用以下代码摆脱它。但是,您最好使用类。

代码:

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());

    /* Reset the colour of all buttons' icons. */
    a.closest("tbody").find("tr button > i").css("color", "");

    /* Make the current button's icon's colour red. */
    a.children("i").css("color", "red");
  });
};

【讨论】:

  • 该解决方案部分有效。如果该图标被按下,它会变成红色,但只是一秒钟。变回初始颜色。我相信这是我遇到的问题,因为所有按钮都具有相同的 ID。这就是为什么我需要通过值而不是 id 来获取它们,这就是为什么我需要 jQuery 而不是 CSS。或者至少这就是我知道的原因。
  • 如果您按住按钮而不释放它,则该解决方案有效。这不是你想要的吗?您希望它在点击时更改并保持更改吗?
  • 顺便说一句,您应该真正解决重复和三重 id 的问题。 ID 旨在唯一地标识一个 HTML 元素。你应该让它们成为类。
  • 对不起,如果我解释得不够清楚。我尝试这样做的原因是为了让用户界面知道他点击了哪个教室的哪个按钮。所以按钮需要保持红色,直到用户刷新或点击同一个按钮,但对于不同的教室,按钮必须再次保持红色。
  • 只要把td#buttonscell改成td.buttonscell,你想要的就变得很简单了。
猜你喜欢
  • 2012-03-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多