繁体   English   中英

React.js document.querySelectorAll() 不返回任何内容

[英]React.js document.querySelectorAll() not returning anything

我基本上是在制作一个 windows xp 主题的投资组合,我很难为我的模态添加标签。 我正在学习一个教程( https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/Tab_Role ),我的问题是当我调用 document.querySelectorAll('[role="tab "]') 我什么也没得到。 使用 querySelector() 返回 null。

我在想这可能是因为在我打开模式之前尚未创建选项卡,但即便如此,事件侦听器也不应该在每次将某些内容添加到 DOM 时检查吗? 也许 scope 是错误的,但是当我将它放在渲染内或 class 内的任何位置时会出现一些错误。

这是我的组件:

import React, { Component } from "react";
import ReactDOM from "react-dom";
import "xp.css/dist/XP.css";
import Draggable from 'react-draggable';


class AboutMeModal extends Component {

    render() {
        return (
            <Draggable
            axis="both"
            handle=".tabs"
            defaultPosition={{x: 40, y: -80}}
            position={null}
            grid={[25, 25]}
            scale={1}
            onStart={this.handleStart}
            onDrag={this.handleDrag}
            onStop={this.handleStop}>

                <section className="tabs" style={{width:'calc(99%)'}}>
                    <menu role="tablist" aria-label="Sample Tabs">
                    <button id="tab-1" role="tab" aria-selected="true" aria-controls="tab-A" tabIndex="0">Tab A</button>
                    <button id="tab-2" role="tab" aria-selected="false" aria-controls="tab-B" tabIndex="-1">Tab B</button>
                    <button id="tab-3" role="tab" aria-selected="false" aria-controls="tab-C" tabIndex="-1">Tab C</button>
                    </menu>
                    <article role="tabpanel" id="tab-A" aria-labelledby="tab-1">
                        <h3>Tab Content</h3>
                        <p>
                            You create the tabs, you would use a <code>menu role="tablist"</code> element then for the tab titles you use a <code>button</code> with the <code>aria-controls</code> parameter set to match the relative <code>role="tabpanel"</code>'s element.
                        </p>
                        <p>
                            Read more at <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/Tab_Role" target="_blank">MDN Web docs - ARIA: tab role</a>
                        </p>
                    </article>
                    <article role="tabpanel" id="tab-B" aria-labelledby="tab-2" hidden>
                        <h3>More...</h3>
                        <p>This tab contains a GroupBox</p>
                    </article>
                    <article role="tabpanel" id="tab-C" aria-labelledby="tab-3" hidden>
                        <h3>Tab 3</h3>
                        <p>Lorem Ipsum Dolor Sit</p>
                    </article>
                </section>

            </Draggable>
        );
    }
    };

export default AboutMeModal; 

window.addEventListener("DOMContentLoaded", () => {
    console.log("event listener added");
    const tabs = document.querySelectorAll('[role="tab"]');    //I never get the list of 
                                                                       //tabs back
    const tabList = document.querySelector('[role="tablist"]');

    // Add a click event handler to each tab
    tabs.forEach(tab => {
        console.log("here"); //my code never reaches here!
        tab.addEventListener("click", changeTabs);
    });
});

function changeTabs(e) {
    console.log("change tabs");
    const target = e.target;
    const parent = target.parentNode;
    const grandparent = parent.parentNode;

    // Remove all current selected tabs
    parent
    .querySelectorAll('[aria-selected="true"]')
    .forEach(t => t.setAttribute("aria-selected", false));

    // Set this tab as selected
    target.setAttribute("aria-selected", true);

    // Hide all tab panels
    grandparent
    .querySelectorAll('[role="tabpanel"]')
    .forEach(p => p.setAttribute("hidden", true));

    // Show the selected panel
    grandparent.parentNode
    .querySelector(`#${target.getAttribute("aria-controls")}`)
    .removeAttribute("hidden");
}

当浏览器知道 HTML 的基本结构时, DOMContentLoaded只会被触发一次。 它在初始加载后再也不会被调用。 您应该直接在按钮选项卡上挂钩您的事件:

<button id="tab-1" onClick={changeTabs} role="tab" aria-selected="true" aria-controls="tab-A" tabIndex="0">Tab A</button>
<button id="tab-2" onClick={changeTabs} role="tab" aria-selected="false" aria-controls="tab-B" tabIndex="-1">Tab B</button>
<button id="tab-3" onClick={changeTabs} role="tab" aria-selected="false" aria-controls="tab-C" tabIndex="-1">Tab C</button>

暂无
暂无

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

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