简体   繁体   English

如何更改绑定到 Razor 页面上模型的特定文本的字体颜色?

[英]How do I change the font color of a specific text bound to a model on a Razor page?

I am building an ASP.NET Core web application and would like to style certain text in my result table a different color than the rest of the table.我正在构建一个 ASP.NET Core Web 应用程序,并希望将我的结果表中的某些文本设置为与表的其余部分不同的颜色。 For example:例如:

期望的结果

I am trying to do this in JavaScript but seem to be running into issues because the data is generated on the page from the model after a Search button is clicked with the selected parameters.我试图在 JavaScript 中执行此操作,但似乎遇到了问题,因为在使用所选参数单击“搜索”按钮后,数据是从模型在页面上生成的。 Here is what I have in my HTML Razor page:这是我的 HTML Razor 页面中的内容:

@foreach (var item in Model.SecurityLog)
{
  <tr>
  <td style="width:7% !important" id="eventType">
      @Html.DisplayFor(modelItem => item.EventType.Name)
  </td>
  <td style="width:9% !important">
      @Html.DisplayFor(modelItem => item.ContactName)
  </td>
}

Here is the JavaScript code that is also on the Razor page:以下是 Razor 页面上的 JavaScript 代码:

 var text = document.getElementById("searchString");
 var str = text.innerHTML;
 var reg = /RL Solution Required/ig;

 var toStr = String(reg);
 var search = (toStr.replace('\/g', '|')).substring(1);
 var searches = search.split("|");

 if (searches.indexOf("RL Solution Required") > -1) {
    str = str.replace(/RL Solution Required/g, '<span style="color:red;">R/L Solution Required</span>');
 }
 document.getElementById("searchString").innerHTML = str;

I am trying to get the RL Solution Required font to be in red.我试图让 RL Solution Required 字体变成红色。 I am not experiencing any errors with this code but the font is black.我没有遇到此代码的任何错误,但字体是黑色的。 Thanks in advance and any help is appreciated.在此先感谢您的帮助,我们将不胜感激。

You don't need to use Javascript for this.您不需要为此使用 Javascript。 Just use Razor.只需使用剃刀。

@foreach (var item in Model.SecurityLog)
{
  <tr>
  <td style="width:7% !important">        
      @{
        string eventType = item.EventType.Name;
      }
      @if(eventType.Contains("RL Solution Required"){
        <span style="color:red;">@Html.DisplayFor(modelItem => eventType)</span>
      }else{
        @Html.DisplayFor(modelItem => eventType)
      }          
  </td>
  <td style="width:9% !important">
      @Html.DisplayFor(modelItem => item.ContactName)
  </td>
}

The issue you're experiencing could be for a number of reasons.您遇到的问题可能有多种原因。

For example: I see you using.important in some of your css, If you have another span tag with a color property that also is set to.important, it will be overriding the color that you are inserting into your html.例如:我看到您在某些 css 中使用了 important,如果您有另一个 span 标签,其颜色属性也设置为 .important,它将覆盖您插入到 html 中的颜色。

The best thing to do would be to inspect the element in a browser's dev tools and try modifying the css to properly update the color there.最好的办法是检查浏览器开发工具中的元素并尝试修改 css 以正确更新那里的颜色。 If you can get it to update there and describe what you have changed, it will be easier to narrow down the specific issue that you are having here.如果您可以让它在那里更新并描述您所做的更改,那么将更容易缩小您在这里遇到的具体问题的范围。

If you ARE able to modify the element properly using css and something needs changed in your generated code, you may be able to just go from there and figure it out.如果您能够使用 css 正确修改元素并且需要在生成的代码中更改某些内容,您可以从那里开始并弄清楚。

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

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