简体   繁体   English

将css类分配给textArea时出现Javascript错误

[英]Javascript error when assigning css class to textArea

I'm having a problem in Safari desktop / mobile and Internet Explorer.我在 Safari 桌面/移动设备和 Internet Explorer 中遇到问题。 The error reads:错误内容如下:

In Safari: TypeError: Attempted to assign to readonly property.在 Safari 中: TypeError: Attempted to assign to readonly property.

IE Edge: Assignment to read-only properties is not allowed in strict mode IE Edge: Assignment to read-only properties is not allowed in strict mode

It is happening in a function that loops through all textareas on the page and sets a specific css class on each textarea according to the character count (which changes the font-size).它发生在一个函数中,该函数循环遍历页面上的所有文本区域,并根据字符数(改变字体大小)在每个文本区域上设置一个特定的 css 类。

My questions are:我的问题是:

  • Why is this error occurring when I'm just applying a css class?为什么在我只是应用 css 类时会发生此错误?
  • What can I do to prevent it?我能做些什么来防止它?

I'm using Laravel 5.4 and VueJS 2.1.10.我正在使用 Laravel 5.4 和 VueJS 2.1.10。

Here is a codepen link where I have replicated the problem.这是我复制问题的codepen链接 (You can see it works in Chrome and FireFox). (您可以看到它在 Chrome 和 FireFox 中有效)。

I will really appreciate any help.我将非常感谢任何帮助。 Thanks谢谢

Don't assign values directly to classList , it's read-only and any ability to modify it by operators is non-standard and unspecified.不要将值直接分配给classList ,它是只读的,并且操作员对其进行修改的任何能力都是非标准且未指定的。

Use classList.add() instead.请改用classList.add()
https://developer.mozilla.org/en-US/docs/Web/API/Element/classList https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

Replace card.classList += 'font_normal';替换card.classList += 'font_normal'; with card.classList.add('font_normal');card.classList.add('font_normal'); as per themozilla api根据Mozilla api

Hope this helps!希望这可以帮助!

You should not be messing with the DOM.你不应该弄乱 DOM。 Your contract with Vue is that it will control the DOM and you will provide it a model that describes how things should appear and work.您与 Vue 的约定是它将控制 DOM,并且您将为它提供一个模型来描述事物应该如何出现和工作。 In this case, you should be using v-bind:class .在这种情况下,您应该使用v-bind:class It looks like you want to do something like看起来你想做类似的事情

 const app = new Vue({ el: '#app', data: { showPublicStacks: [{ id: 1, public_category_title: "Spanish 101" }, { id: 3, public_category_title: "African Capitals. Plus extra characters to test smaller font" }, { id: 2, public_category_title: "USA Capitals" } ] } });
 #app { display: flex; width: 100%; } .width_33 { flex: 0 0 33.333%; } /* Fonts */ .font_normal { font-size: 2em; } .font_small { font-size: 1em; } /* Cards */ div.media_container { background: #ffffff; padding: 10px; box-shadow: 0 2px 7px rgba(0, 0, 0, .2); transition: .2s ease-in; border-radius: 4px; } .card_input_area { width: 100%; height: 100px; margin-top: 10px; padding-top: 10px; border: none; font-family: sans-serif; color: grey; background-color: #ffffff; text-align: center; padding: 20px; transition: 300ms ease-in; -webkit-transition: 300ms ease-in; animation: show_text 210ms; cursor: pointer; }
 <script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.3.3/vue.min.js"></script> <div id="app"> <span class="width_33" v-for="stack in showPublicStacks" :stack="stack" :id="'column_'+stack.id" v-bind:key="stack"> <div class="media_container"> <textarea class="card_input_area" :class="stack.public_category_title.length > 35 ? 'font_small' : 'font_normal'" v-model.trim="stack.public_category_title"></textarea> </div> </span> </div>

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

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