简体   繁体   English

使用classList从JavaScript动态添加Web组件中的类

[英]add class dynamically in web components from JavaScript using classList

How to add dynamic styles to the shadow DOM 如何向阴影DOM添加动态样式

index.html index.html

<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
    <link rel="import" href="nav-component.html">
</head>
<body>
    <app-nav></app-nav>
</body>
</html>

nav-component.html nav-component.html

<template>
  <style>
    .btn {
        background-color: green;
        padding: 10px 20px;
    }
  </style>
    <button onclick="getstyles()">ENTER</button>
</template>
<script src="nav-component.js"></script>

nav-component.js nav-component.js

let template = document.currentScript.ownerDocument.querySelector('template');
let clone = document.importNode(template.content, true);
let proto = Object.create(HTMLElement.prototype);

proto.createdCallback = function () {
    let root = this.createShadowRoot();
    root.appendChild(clone);
}
document.registerElement('app-nav', {
    prototype: proto
});

function getstyles() {
    console.log('it works');
    document.querySelector('button').classList.add('btn');
    // document.currentScript.ownerDocument.querySelector('button').classList.add('btn');
}

have to add btn class to button element , so that its styles added to button element 必须将btn类添加到button元素 ,以便将其样式添加到button元素

got error Uncaught TypeError: Cannot read property 'classList' of null 错误Uncaught TypeError:无法读取null的属性“ classList”

Demo 演示版

First of all document.registerElement is deprecated so I answered class based custom element solution here... 首先不建议使用document.registerElement ,因此我在这里回答了基于类的自定义元素解决方案...

The solution is to get the document from document.currentScript.ownerDocument 解决方案是从document.currentScript.ownerDocument获取document.currentScript.ownerDocument

class AppNavElement extends HTMLElement {
    constructor() {
        super()

        // import from ownerDocument
        const importDoc = document.currentScript.ownerDocument;
        let shadowRoot = this.attachShadow({mode: 'open'});
        const template = importDoc.querySelector('#template');
        const instance = template.content.cloneNode(true);
        shadowRoot.appendChild(instance);

        // Event Listener
        this.addEventListener('click', e => this.changeStyle());
    }

    changeStyle() {
        this.shadowRoot.querySelector('button').classList.add('btn')
    }
}

customElements.define('app-nav', AppNavElement)

Update: 更新:

To listen the button element only use the connectedCallback thanks @bhv 要只听按钮元素,请使用connectedCallback谢谢@bhv

connectedCallback() { 
    let btn = this.shadowRoot.querySelector('button') 
    // Event Listener 
    btn.addEventListener('click', e => this.changeStyle()); 
}

You can also simply get the <button> element from the event.target property : 您还可以简单地从event.target属性获取<button>元素:

function changeStyle() {
    console.log('it works');
    event.target.classList.add('btn');
}

...

<button onclick="changeStyle()">ENTER</button>

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

相关问题 JavaScript:在classList中添加特定于类的标签 - JavaScript: Add a class specific tag in classList 如何使用香草 Javascript 添加 classList? - How to add classList using vanilla Javascript? 将课程添加到课程列表 - Add class to classlist 当 javascript 中的 element.classList.add() 方法添加新的 class 时,无法使用 DOM 操作设置元素样式 - Cannot style elements using DOM manipulation when new class is added by element.classList.add() method in javascript 如何使用.classList.add('class')在具有多个相同class名称的元素与javaScript中的其他元素添加样式 - How to add style on element which have multiple same class name with other elements in javaScript using .classList.add(‘class’) 如何在“element.classList.add”中切换“添加”以使用 javascript 删除 - How to toggle 'add' in "element.classList.add" to remove using javascript 无法使用 javascript classList 在选择选项中添加或更改类 - Cannot add or change class in select options with javascript classList 如何使用JavaScript动态添加类 - How to add class dynamically using javascript 使用 styled-components 更改其 classList 动态更改的组件的样式 - Changing the style of a component that has its classList dynamically changed, using styled-components Javascript classList.add不起作用 - Javascript classList.add is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM