javascript - 如何在 jquery/javascript 中更改点击文本的颜色?

标签 javascript jquery html css

我有一个 fiddle我已通过查看下面的屏幕截图复制了它。

enter image description here

fiddle 的工作方式是点击 2 个方框,底部会出现另一个框。

为了得到它我使用的 JQuery 代码:

$("#franchisehub").click(function() {
  if ($('.franchisehubtv').css('display') == "flex") {
    $('.franchisehubtv').css('display', 'none');

  } else {
    $('#franchisehub').css('background-color', 'green'); 
    $('#franchisehub p').css('color', 'white');  // Added line
    $('#cloudbasedmobile').css('background-color', 'white');    
    $('#businessanalytics').css('background-color', 'white');   
    $('#techsupport').css('background-color', 'white'); 
    $('#ordermanagement').css('background-color', 'white');  
    $('#employeemanagement').css('background-color', 'white'); 
    $('#whitelabel').css('background-color', 'white');
    $('#emailmarketing').css('background-color', 'white');   
    $('#royaltycalculator').css('background-color', 'white');  
    $('#customizationtools').css('background-color', 'white');
    $('#goalsetting').css('background-color', 'white');  
    $('#custominvoicing').css('background-color', 'white'); 
    $('#leadtracking').css('background-color', 'white');
    $('#brandcontrol').css('background-color', 'white');    


    $('.franchisehubtv').css('display', 'flex'); 
    $('.cloudbasedtextipad').css('display', 'none');
    $('.business-analytics').css('display', 'none');
    $('.tech-support').css('display', 'none');
    $('.order-management').css('display', 'none');
    $('.employee-management').css('display', 'none');
    $('.white-label').css('display', 'none');
    $('.brand-control').css('display', 'none');
    $('.lead-tracking').css('display', 'none');
    $('.custom-invoicing').css('display', 'none');
    $('.goal-setting').css('display', 'none');
    $('.customization-tools').css('display', 'none');
    $('.royalty-calculator').css('display', 'none');
    $('.email-marketing').css('display', 'none');
  }

});

问题陈述:

我想要完成的是,点击上面屏幕截图中显示的绿色图像,文本颜色 (Franchise Hub 或 Cloud Based & Mobile) 应在 2 个方框内变为白色。

为了实现这一点,我使用了以下行,但它似乎不起作用。

$('#franchisehub p').css('color', 'white');//添加行

最佳答案

嘿,请检查我的 codepen 中的代码所以基本上,如果您将两个文本颜色都更改为白色,那么它在选定的(绿色图像)中工作正常,但在另一个框中背景为白色,因此文本消失。所以我只将所选(绿色图像)框的 p 文本更改为白色。我编辑了以下两个点击功能

$("#franchisehub").click(function() {
  if ($('.franchisehubtv').css('display') == "flex") {
    $('.franchisehubtv').css('display', 'none');

  } else {
    $('#franchisehub').css('background-color', 'green'); 
    $('#franchisehub p').css('color', 'blue !important'); 
  $('#franchisehub').find('p').css({color: '#fff'});
  $('#cloudbasedmobile').find('p').css({color: '#222'});
    $('#cloudbasedmobile').css('background-color', 'white');    
    $('#businessanalytics').css('background-color', 'white');   
    $('#techsupport').css('background-color', 'white'); 
    $('#ordermanagement').css('background-color', 'white');  
    $('#employeemanagement').css('background-color', 'white'); 
    $('#whitelabel').css('background-color', 'white');
    $('#emailmarketing').css('background-color', 'white');   
    $('#royaltycalculator').css('background-color', 'white');  
    $('#customizationtools').css('background-color', 'white');
    $('#goalsetting').css('background-color', 'white');  
    $('#custominvoicing').css('background-color', 'white'); 
    $('#leadtracking').css('background-color', 'white');
    $('#brandcontrol').css('background-color', 'white');    


    $('.franchisehubtv').css('display', 'flex'); 
    $('.cloudbasedtextipad').css('display', 'none');
    $('.business-analytics').css('display', 'none');
    $('.tech-support').css('display', 'none');
    $('.order-management').css('display', 'none');
    $('.employee-management').css('display', 'none');
    $('.white-label').css('display', 'none');
    $('.brand-control').css('display', 'none');
    $('.lead-tracking').css('display', 'none');
    $('.custom-invoicing').css('display', 'none');
    $('.goal-setting').css('display', 'none');
    $('.customization-tools').css('display', 'none');
    $('.royalty-calculator').css('display', 'none');
    $('.email-marketing').css('display', 'none');
  }

});

$("#cloudbasedmobile").click(function() {

  if ($('.cloudbasedtextipad').css('display') == "flex") {
    $('.cloudbasedtextipad').css('display', 'none');

  } else {
    $('#franchisehub').css('background-color', 'white'); 
    $('#cloudbasedmobile').css('background-color', 'green');    
    $('#businessanalytics').css('background-color', 'white');   
    $('#techsupport').css('background-color', 'white'); 
    $('#ordermanagement').css('background-color', 'white');  
    $('#employeemanagement').css('background-color', 'white'); 
    $('#whitelabel').css('background-color', 'white');
    $('#emailmarketing').css('background-color', 'white');   
    $('#royaltycalculator').css('background-color', 'white');  
    $('#customizationtools').css('background-color', 'white');
    $('#goalsetting').css('background-color', 'white');  
    $('#custominvoicing').css('background-color', 'white'); 
    $('#leadtracking').css('background-color', 'white');
    $('#brandcontrol').css('background-color', 'white');    

$('#franchisehub').find('p').css({color: '#222'});

$('#cloudbasedmobile').find('p').css({color: '#fff'});

    $('.cloudbasedtextipad').css('display', 'flex');
    $('.franchisehubtv').css('display', 'none'); 
    $('.business-analytics').css('display', 'none');
    $('.tech-support').css('display', 'none');
    $('.order-management').css('display', 'none');
    $('.employee-management').css('display', 'none');
    $('.white-label').css('display', 'none');
    $('.brand-control').css('display', 'none');
    $('.lead-tracking').css('display', 'none');
    $('.custom-invoicing').css('display', 'none');
    $('.goal-setting').css('display', 'none');
    $('.customization-tools').css('display', 'none');
    $('.royalty-calculator').css('display', 'none');
    $('.email-marketing').css('display', 'none');

  }
});

关于javascript - 如何在 jquery/javascript 中更改点击文本的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50624637/

相关文章:

javascript - HTML Canvas 绘制形状

javascript - 没有为 FingerprintJS 定义 ReferenceError 导航器

jquery - 有什么方法可以用 CSS/jQuery 手动排列列表项(只对它们排序一次,而不是四处拖动)?

html - 如何使不同的div在CSS中具有不同的内容颜色

javascript - 如何在使用 jquery 中的类删除元素时继承事件处理程序?

jquery - 使图像在 div 内可拖动并可调整大小

javascript - Meteor 中的 CSS 问题?

javascript - 将多个文件注入(inject)书签

jquery - 表格中的点击事件 <td> - 改变颜色

Jquery 自动完成动态文本框问题