简体   繁体   English

较少-定义与Mixin冲突的类名

[英]Less - Defining a class name that conflicts with mixin

The problem that I've got is quite simple. 我遇到的问题很简单。 I want to use a css class name that has been previously defined as a mixin. 我想使用先前已定义为mixin的CSS类名称。

I'm currently using Bootstrap , which has defined a mixin called "placeholder", and because of backwards compatibility we're planning to use the jQuery placeholder plugin to support placeholders for IE 9. The problem is that this plugin adds the css class "placeholder" to the DOM element. 我目前正在使用Bootstrap ,它已定义了一个称为“占位符”的混合,并且由于向后兼容,我们正计划使用jQuery占位符插件来支持IE 9的占位符。问题是该插件添加了css类“占位符”。

How do I define the css class "placeholder" in my LESS file without executing the mixin previously defined in Bootstrap? 如何在我的LESS文件中定义css类“占位符”而不执行先前在Bootstrap中定义的mixin?

This should not be a problem. 这应该不是问题。 This is bootstrap's placeholder mixin 这是bootstrap的占位符mixin

// Placeholder text
.placeholder(@color: @input-color-placeholder) {
  &:-moz-placeholder            { color: @color; } // Firefox 4-18
  &::-moz-placeholder           { color: @color;   // Firefox 19+
                              opacity: 1; } // See https://github.com/twbs/bootstrap/pull/11526
  &:-ms-input-placeholder       { color: @color; } // Internet Explorer 10+
  &::-webkit-input-placeholder  { color: @color; } // Safari and Chrome
}

This is not a mixin class, as it has parenthesis after it. 这不是mixin类,因为它后面带有括号。 Therfore it will not output anything unless it is referenced. 因此,除非被引用,否则它将不会输出任何内容。 To use this I would need to do something like this. 要使用此功能,我需要执行以下操作。

.input {
    .placeholder;
}

Therefore this will not interfere with any .placeholder class you may have. 因此,这不会干扰您可能拥有的任何.placeholder类。

.placeholder {
    color: red;
}

The two should happily co-exist and you should not get any problems. 两者应该愉快地并存,并且您不会遇到任何问题。

Edit: see comments 编辑:查看评论

The solution was to use multi class selector to overcome the issue. 解决方案是使用多类选择器来解决该问题。

Example

.input.placeholder {
    color: red;
}

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

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