简体   繁体   English

命名空间切换CSS类

[英]namespaced toggling for CSS classes

jquery has this function jQuery具有此功能

but it does not allow name-spacing. 但它不允许名称间隔。 With name-spacing you can toggle between multiple classes as if they were radio buttons. 使用名称间距,您可以在多个类之间切换,就好像它们是单选按钮一样。

for example 例如

toggleClass(el, 'ns1', 'test')

will check ns1 and will remove any other class with this namespace and replace it with the class test 将检查ns1并将使用此命名空间删除任何其他类,并将其替换为类test

I wrote this library function to keep my application code. 我写了这个库函数来保留我的应用程序代码。

One mistake I made was I prefixed the class with the namespace rather then make it a separate argument. 我犯的一个错误是,我给类添加了名称空间前缀,而不是将其作为单独的参数。

Here is my in progress use of a custom toggleClass with name-spacing: 这是我正在使用的具有名称空间的自定义toggleClass:

// apply event listeners to the input elements
applyEL: function (input_element, label_element) {
    var self = this;
    self.J.input = $(input_element);
    self.J.input.on("blur", function () {
        if (input_element.value === '') {
            $A.toggleClass(input_element, self.S.toggle_border_show);
            $A.toggleClass(label_element, self.S.toggle_label_show);
            $A.expandFont(label_element, 'up', self.S.speed);
        }
    }, false);
    self.J.input.on("focus", function () {
        if (input_element.value === '') {
            $A.toggleClass(input_element, self.S.toggle_border_obscure);
            $A.toggleClass(label_element, self.S.toggle_label_obscure);
        }
    }, false);
    self.J.input.on("paste keypress drop", function () {
        $A.setTimeout(function (label_element, input_element) {
            $A.toggleClass(label_element, this.S.toggle_label_hide);
            $A.toggleClass(input_element, this.S.toggle_border_hide);
        }, 0);
    }, false);
}

Is there a better way to do this, other from what I mentioned. 除了我提到的以外,还有更好的方法吗?

Better way would be, rather add a class to html or body element, which would act as a name space, much like common theme switchers? 更好的方法是,将类添加到html或body元素中,该类将充当名称空间,就像常见的主题切换器一样?

And if you need a local name space for a specific element you would reference the parent element. 而且,如果您需要特定元素的本地名称空间,则可以引用父元素。

And boom, you have name spaces! 和繁荣,你有名字空间!

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

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