可以在JavaScript中更改其名称的访问元素可以

发布于 2025-02-06 06:37:43 字数 723 浏览 3 评论 0原文

我想将元素类的类别从 score -box1 更改为 score -box1. -Active 。我创建了一个const $ target,以尝试访问类得分-box1,但它不起作用。

const $target = document.getElementByClassname('score -box1')
function PlayerScore(score = 0){
    if(score == 1){
        $target.toggle('-active');
    }else if(score == 2){
        $target.toggle('-active');
    }else if(score == 3){
        $target.toggle('-active');
    }

    return( /*html*/
    `
        <div class = "container">
            <div class = "score -box1">one</div>
            <div class = "score -box2">two</div>
            <div class = "score -box3">three</div>
        </div>
    `
    )
}

I'd like to change an element's class from score -box1 to score -box1.-active. I created a const $target to try to access the class score -box1 but its not working.

const $target = document.getElementByClassname('score -box1')
function PlayerScore(score = 0){
    if(score == 1){
        $target.toggle('-active');
    }else if(score == 2){
        $target.toggle('-active');
    }else if(score == 3){
        $target.toggle('-active');
    }

    return( /*html*/
    `
        <div class = "container">
            <div class = "score -box1">one</div>
            <div class = "score -box2">two</div>
            <div class = "score -box3">three</div>
        </div>
    `
    )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倦话 2025-02-13 06:37:44

正确的方法是.querySelectorall,并且它返回节点列表,如果您在下面的末尾添加[0],您将获得该节点列表的第一个元素(也许您现在想要的),但是,如果您想要所有目标元素使用for loop通过document.queryselectorall('scorce -box1')循环循环。

const $target = document.querySelectorAll('score -box1')[0]
function PlayerScore(score = 0){
    if(score == 1){
        $target.toggle('-active');
    }else if(score == 2){
        $target.toggle('-active');
    }else if(score == 3){
        $target.toggle('-active');
    }

    return( /*html*/
    `
        <div class = "container">
            <div class = "score -box1">one</div>
            <div class = "score -box2">two</div>
            <div class = "score -box3">three</div>
        </div>
    `
    )
}

或者,您可以用.querySelectorAll使用.querySelector,它将自动删除第一个元素: https://www.w3schools.com/jsref/met_document_queryselector.asp

The proper method is .querySelectorAll and it returns a node list , if you add a [0] at the end like below, you will get the first element of that node list (which maybe what you want for now), but if you want all target elements use a for loop to loop over document.querySelectorAll('score -box1') array.

const $target = document.querySelectorAll('score -box1')[0]
function PlayerScore(score = 0){
    if(score == 1){
        $target.toggle('-active');
    }else if(score == 2){
        $target.toggle('-active');
    }else if(score == 3){
        $target.toggle('-active');
    }

    return( /*html*/
    `
        <div class = "container">
            <div class = "score -box1">one</div>
            <div class = "score -box2">two</div>
            <div class = "score -box3">three</div>
        </div>
    `
    )
}

Or you can replace the .querySelectorAll with with .querySelector, it will automatically rturn the first element: https://www.w3schools.com/jsref/met_document_queryselector.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文