简体   繁体   English

当 LWC JS 中超过最大字符限制时,有没有办法在 Lightning 输入富文本字段中更改字体颜色?

[英]Is there a way to change a font color in Lightning input rich text field when maximum character limit is exceeded in LWC JS?

I'm trying to get the character count and on that basis want to change the color of font.我正在尝试获取字符数,并在此基础上想要更改字体的颜色。

I have tried with queryselector() but it is not working:我试过queryselector()但它不起作用:

handleChange(event) {
         if( this.charCount > 100){        
              let textcolor = this.template.queryselector("lightning-input-rich-text");
                 textcolor.getformat({color:"red"});
              }
        }
        

I have also tried with:我也试过:

        handleChange(event) {
         if( this.charCount > 100){  
               textcolor = this.template.queryselector('[data-id="summary"]');
           if(textcolor) 
                    this.template.querySelector('[data- 
 id="summarytextcolor"]').className='summarytext';
                 
        }
        }

Here is my CSS:这是我的 CSS:

.summarytext {color: #ff0000;}

And here is my lightning code, where I have taken a rich text field where on change I need to capture the number of character count:这是我的闪电代码,我在其中获取了一个富文本字段,在更改时我需要捕获字符数:

<lightning-input-rich-text
         data-id="summary"
        label={label.Summary}
        formats={richTextformats}
        label-visible
         valid={richTextValidity}
         message-when-bad-input={richTextErrorMessage}
        class="slds-form-element slds-form-element_stacked summarytext"
        onchange={handleChange}
        value={richTextValue}
        style=" font-weight: normal;">
    </lightning-input-rich-text>

Here is the error I'm getting:这是我得到的错误:

[NoErrorObjectAvailable] Script error.    a()@https://static.lightning.force.com/cs10/auraFW/javascript/7p9HLMpgnV2GO9MqZhXGUw/aura_prod.js:948:169 {anonymous}()@https://static.lightning.force.com/cs10/auraFW/javascript/7p9HLMpgnV2GO9MqZhXGUw/aura_prod.js:948:362 bt.dispatchEvent()@https://static.lightning.force.com/cs10/auraFW/javascript/7p9HLMpgnV2GO9MqZhXGUw/aura_prod.js:12:12146 bt.dispatchChangeEvent()@https://customization-velocity-2311-dev-ed.lightning.force.com/components/interop/inputRichText.js:2:28144 eval()@https://customization-velocity-2311-dev-ed.lightning.force.com/components/interop/inputRichText.js:2:20578 e.l.emit()@https://customization-velocity-2311-dev-ed.lightning.force.com/components/lightning/quillLib.js:2:142032 e.value()@https://customization-velocity-2311-dev-ed.lightning.force.com/components/lightning/quillLib.js:2:33139

What's the problem, and how can I fix it?有什么问题,我该如何解决?

You should be able to set it using vanilla javascript.您应该可以使用 vanilla javascript 设置它。

handleChange(event) {
        this.loading = true;
        if(this.charCount > 100){        
             var warn = richTextValue.fontcolor("Red");
             this.richTextValue = warn;
        }
        this.loading = false;
}

I added a variable 'loading';我添加了一个变量“加载”; just make sure the input rich text is wrapped in a <template if:false={loading}> so the color of the font gets re-rendered.只需确保输入的富文本包含在<template if:false={loading}>以便重新渲染字体的颜色。

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

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