简体   繁体   English

我不知道如何在 Javascript 工具包框架中处理选择 HTML 类属性

[英]I don´t know how select HTML Class attribute handled in Javascript toolkit Framework

I am using the Electron Framework in Javascript called Xel toolkit, and I have the following syntax from main.js :我在 Javascript 中使用了名为Xel工具包的Electron框架,并且我从main.js有以下语法:

document.querySelector("menu.selected").className.remove('selected')

Having some Xel code where selected is a pure HTML Class:选择一些Xel代码是一个纯 HTML 类:

<x-tab selected class="menu">

You know, it is a bad practice something just like this, without type a Class="name" .你知道,像这样不输入Class="name"是一种不好的做法。 But I don't know how I can catch the selected current value.但我不知道如何捕捉选定的当前值。 Checking Console:检查控制台:

Uncaught TypeError: Cannot read property 'className' of null

In your example selected in that context is not a "pure HTML Class".在您在该上下文中selected的示例中,不是“纯 HTML 类”。 That is an attribute equivalent to selected="true" in HTML5.这是一个等同于 HTML5 中selected="true"的属性。 If you want to use querySelector to find that element and remove the selected attribute with Javascript, you'll need something like this:如果要使用querySelector查找该元素并使用 Javascript 删除selected属性,则需要如下内容:

document.querySelector("menu[selected]").selected = false;

I'm not sure what exactly you are trying to accomplish here, but I think this may be of some help.我不确定你到底想在这里完成什么,但我认为这可能会有所帮助。 the following code sample that I am showing you will grab the attribute of checked based on className which I think is what you are ultimately asking for...我向您展示的以下代码示例将根据 className 获取检查的属性,我认为这是您最终要求的...

ClickToSeeJsBin ClickToSeeJsBin

https://jsbin.com/kahona/edit?html,output https://jsbin.com/kahona/edit?html,输出

    function validate() {
    var CurrentSelected = [];
    //Doing the same thing for checkBox selection
    var GetAllCheckBoxNodes = document.getElementsByClassName("someCheckBox");
    var InputNodesSize = GetAllCheckBoxNodes.length;
    for (var i = 0; i < InputNodesSize; i++) {
        var CurrentCheckBox = GetAllCheckBoxNodes[i];
        if (CurrentCheckBox.checked) {
            CurrentSelected.push(CurrentCheckBox.value);
        }
    }
    //Doing the same thing for radio selection
    var GetAllRadioNodes = document.getElementsByClassName("someRadio");
    var GetAllRadioNodesLength = GetAllRadioNodes.length;
    for (var i = 0; i < GetAllRadioNodesLength; i++) {
        var CurrentRadioNode = GetAllRadioNodes[i];
        if (CurrentRadioNode.checked) {
            CurrentSelected.push(CurrentRadioNode.value);
        }
    }
    //Displaying what is Selected...
    CurrentSelected.forEach(item => { console.log(item); });
}

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

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