简体   繁体   English

如何删除使用 jquery 添加的样式属性

[英]How to remove style attribute added with jquery

I am using a devExpress table with some custom requirements.我正在使用具有一些自定义要求的 devExpress 表。

(UPDATE) Took a break from this for a day and went back and did it properly using React Styling! (更新)休息了一天,然后返回并使用 React 样式正确完成! Thanks for suggestions感谢您的建议

截屏

In the screenshot I have certain cells disabled.在屏幕截图中,我禁用了某些单元格。 However the user wants all cells to look disabled other that the row selected.但是,用户希望所有单元格看起来都被禁用,而不是选定的行。

Using this使用这个

   window
  .$("td")
  .not(document.getElementById(this.state.selection[0]))
  .not(document.getElementsByClassName(this.state.selection[0]))
  .not("td:first-child")
  .not(window.$("td:contains('iPlay')"))
  .not(window.$("td:contains('iLOE')"))
  .not(window.$("td:contains('iInvest')"))
  .not(window.$("td:contains('SPACER')"))
  .not(window.$("td:contains('$MM')"))
  .not(window.$("td:contains('$/BOE')"))
  .attr("style", "color:#868a8f");
window
  .$("td > div > div > div > input")
  .not(document.getElementsByClassName(this.state.selection[0]))
  .attr("style", "color:#868a8f");

I managed to achieve my desired result on page load我设法在页面加载时达到了我想要的结果屏幕截图2

My problem is when I select a new row I cannot remove that color I applied before when it was not selected.我的问题是当我 select 一个新行时,我无法删除我之前应用的颜色,但它没有被选中。 I am trying to use "has" to find the selected row and change the color back to inherit or completely remove the style attribute.我正在尝试使用“has”来查找选定的行并将颜色更改回继承或完全删除样式属性。

    window
  .$("td")
  .has(document.getElementById(this.state.selection[0]))
  .has(document.getElementsByClassName(this.state.selection[0]))
  .not("td:first-child")
  .not(window.$("td:contains('iPlay')"))
  .not(window.$("td:contains('iLOE')"))
  .not(window.$("td:contains('iInvest')"))
  .not(window.$("td:contains('SPACER')"))
  .not(window.$("td:contains('$MM')"))
  .not(window.$("td:contains('$/BOE')"))
  .attr("style", "color:inherit");
window
  .$("td > div > div > div > input")
  .has(document.getElementsByClassName(this.state.selection[0]))
  .attr("style", "color:inherit");

If it helps I do have the ids of the rows that are NOT selected.如果有帮助,我确实有未选择的行的 ID。 I tried to do something with that but did not have any luck我试图用它做点什么,但没有任何运气

  const otherRows = ExpensesUtils.ROW_PROPS.filter(x => x !== this.state.selection[0]);
for (let i = 0; i < otherRows.length; i += 1) {
  window
  .$("td")
  .has(document.getElementById(otherRows[i]))
  .has(document.getElementsByClassName(otherRows[i]))
  .attr("style", "color:inherit");
  window
  .$("td > div > div > div > input")
  .has(document.getElementById(otherRows[i]))
  .has(document.getElementsByClassName(otherRows[i]))
  .attr("style", "color:inherit");
}

link to HTML Table HTML链接到 HTML表 HTML

this.state.selection[0] is the selected rowId from the list below this.state.selection[0] 是从下面的列表中选择的 rowId

I have applied the the rowIds to classes in the nested components.我已将 rowIds 应用于嵌套组件中的类。 I could not figure out another way to access them.我想不出另一种访问它们的方法。

  const ROW_PROPS = [
  "leaseAndWellExpense",
  "leaseAndWellExpenseBoe",
  "iloeLeaseAndWellExpense",
  "iloeLeaseAndWellExpenseBoe",
  "gnaLeaseAndWell",
  "gnaLeaseAndWellBoe",
  "transportation",
  "transportationBoe",
  "divisionGnA",
  "divisionGnABoe",
  "gatheringProcessing",
  "gatheringProcessingBoe",
  "hqGnA",
  "hqGnABoe",
  "interestExpense",
  "interestExpenseBoe",
  "netProdBoe",
  "leaseImpairments",
  "leaseImpairmentsBoe",
  "ddaProducing",
  "ddaProducingBoe",
  "iInvestDdaProducing",
  "iInvestDdaProducingBoe",
  "ddaGatheringProcessing",
  "ddaGatheringProcessingBoe",
  "iInvestDdaGatheringProcessing",
  "iInvestDdaGatheringProcessingBoe",
  "marketingCosts",
  "otherIncomeExpense",
  "otherIncomeExpenseBoe",
  "otherRevenue",
  "incomeTaxProvision",
  "incomeTaxProvisionBoe",
  "severanceTaxes",
  "severanceTaxesPercent",
  "currentTaxes",
  "currentTaxesRate",
  "netWellHeadRevenue",
];

The easiest way of doing this is by creating a CSS rule's stylesheet.最简单的方法是创建 CSS 规则的样式表。

In that stylesheet, you should define 2 classes .在该样式表中,您应该定义2 个 classes

Let's suppose 1 for your desired CSS rules and the other for the default/none rules.假设 1 用于您想要的 CSS 规则,另一个用于默认/无规则。

I am just showing you the simplest version of doing this thing but with another aspect.我只是向你展示做这件事的最简单的版本,但有另一个方面。

 $('#b1').on('click', function() { $('.c1').removeClass('c1'); $(this).addClass('c2'); });
 .c1 { color: red; }.c2 { color: green; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="b1">Change</button> <p class="c1">This is a Test Line.</p>

The easiest way is最简单的方法是

 $('#idName').on('click', function()
{
    $('.className').removeClass('removeClassName');
    $(this).addClass('addClassName');
});

The code above means that when a button with the id of IdName is clicked, the element with className will be removing the class of removeClassName , and adding the class of addClassName .上面的代码意味着当一个 id 为IdName的按钮被点击时,具有 className 的元素将移除 removeClassName 的removeClassName ,并添加addClassName的 class 。

For further clarification you can have a look at Removing CSS Using JQuery Documentation如需进一步说明,您可以查看使用 JQuery 文档删除 CSS

There is another way by which you can achieve it.还有另一种方法可以实现它。 Instead of playing with style attribute, since it takes the highest specificity so somewhere it might create an issue.而不是使用样式属性,因为它需要最高的特异性,所以在某个地方它可能会产生问题。 Instead of that you can use toggleClass .取而代之的是,您可以使用toggleClass First add your default styling to table, whenever you click any row you can make use of toggle class Toggle Class Example首先将默认样式添加到表中,每当您单击任何行时,您都可以使用切换 class 切换 Class 示例

Example.例子。

$("td > div > div > div").click(function(){
  $("input").toggleClass("main");

}) })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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