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

问题描述

我正在尝试获取字符数,并在此基础上想要更改字体的颜色。

我尝试过使用queryselector(),但是它不起作用:

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

我也尝试过:

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

这是我的CSS:

.summarytext {color: #ff0000;}

这是我的闪电代码,我在其中输入了一个丰富的文本字段,在发生变化时,我需要捕获字符数:

<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>

这是我遇到的错误

[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

有什么问题,我该如何解决

解决方法

您应该可以使用 vanilla javascript 进行设置。

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

我添加了一个变量“加载”;只需确保输入的富文本包含在 <template if:false={loading}> 中,以便重新呈现字体的颜色。