简体   繁体   English

在阴影根中添加类到元素

[英]Adding class to element inside shadow root

I'm simply trying to add a class to an element inside a shadow root. 我只是想在阴影根中添加一个元素。 Im using coffeescript to do this inside atom... Here is the problem snippet taken from within my class. 我正在使用coffeescript在atom中执行此操作...这是从我的班级中获取的问题片段。

createChatTab: (chatTitle = defaultChatTitle) ->
  chatTab = document.createElement('gh-chat-tab')
  chatTab.id = "thisWillBeAUniqueID"
  chatTab.innerText = chatTitle

# Add click event
chatTab.addEventListener 'click', =>
  @.className = "open"

# Add to shadowDom
@theBar.appendChild(chatTab)

# Clear the chat tab
chatTab = null

I've created the element, added a click event to it, which I'm then appending to my shadow root element and then clearing the original variable. 我创建了元素,向它添加了一个click事件,然后我将其附加到我的shadow根元素然后清除原始变量。

In run time the element appears fine within the shadow root, and a console log shows that the click event did run, however the 'open' class is not added? 在运行时,元素在阴影根中显示正常,控制台日志显示click事件确实运行,但是没有添加“open”类?

Solved: Managed to wrangle this one myself: Answered below. 解决:自己管理这个问题:下面回答。

Managed to solve this by changing fat area to thin arrow! 管理通过将脂肪区域改为细箭来解决这个问题!

# Add click event
  chatTab.addEventListener 'click', ->
  @.className = 'open'

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

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