简体   繁体   English

jQuery:检查元素是否具有特定样式(非内联)

[英]jQuery: check if element has specific style (not inline)

Is it possible to check via jQuery or vanilla JS if an element has a specific style? 是否可以通过jQuery或Vanilla JS检查某个元素是否具有特定样式?

In my case I want to check if any input-fields on the page have a red border — applied via an external CSS-File. 就我而言,我想检查页面上的任何输入字段是否带有红色边框-通过外部CSS文件应用。 No inline-css and no style -attr is available, styling is completely external. 没有inline-css,也没有style -attr,样式完全是外部的。

For my initial testing I got the following code from this StackOverflow-Answer: https://stackoverflow.com/a/29659187 对于我的初始测试,我从此StackOverflow-Answer中获取了以下代码: https : //stackoverflow.com/a/29659187

$('.formValidation input[type=submit]').on('click',function(e){
  e.preventDefault();
  var res = $('.formValidation input[type=text],.formValidation input[type=email],.formValidation input[type=url],.formValidation input[type=password]').toArray().some(function(el){
    return $(el).css('border-color') === 'rgb(255,0,0)'
  });
  if (res) {
    console.log('Still at least one field to go!');
  } else {
    console.log('You can submit!');
  }
});

… but .css seams to only test inlineStyles. …但是.css接缝只能测试inlineStyles。

Update I can't change HTML, the markup has to stay «as is». 更新我无法更改HTML,标记必须保持“原样”。 The red border is coming through css only. 红色边框仅通过CSS。 Like this: https://jsfiddle.net/z3t6k04s/ 像这样: https : //jsfiddle.net/z3t6k04s/

You could use 你可以用

Window.getComputedStyle() Window.getComputedStyle()

The Window.getComputedStyle() method gives the values of all the CSS properties of an element after applying the active stylesheets and resolving any basic computation those values may contain. 在应用活动样式表并解决这些值可能包含的所有基本计算之后,Window.getComputedStyle()方法给出元素的所有CSS属性的值。

Example: 例:

 var inp = document.getElementsByTagName('input')[0]; var style = window.getComputedStyle(inp, null); console.log(style.getPropertyValue("border-color")) 
 input[type='text'] { border: 1px solid rgb(255, 0, 0); } 
 <input type="text" value="Foo" /> 

Try it like this: 像这样尝试:

 $('.formValidation input[type=submit]').on('click',function(e){
        // Prevent Default Form Submit
         e.preventDefault();

        // Define Global if Invalid Fields Exist
        var hasInvalidInputs = false;

        // Run Through all to check Input Fields
        $('input').filter(function(index){

          // Check if Invalid Inputs already got detected. If not - check if this field is red
          if( !hasInvalidInputs )
            hasInvalidInputs = $(this).css('border-color') == 'rgb(161, 0, 0)';     // If field is red -> update global var to true
          });

        // Check if Invalid Fields are set to true
        if (hasInvalidInputs) {
          console.log('Still at least one field to go!');
        } else {
          console.log('You can submit!');
        }
    });

Create a one class which has a red color like 创建一个具有红色的类,例如

.red-color{
 border-color:red;
}

$('.formValidation input[type=submit]').on('click',function(e){
  e.preventDefault();
  var res = $('.formValidation input[type=text],.formValidation input[type=email],.formValidation input[type=url],.formValidation input[type=password]').toArray().some(function(el){
    return $(el).hasClass('red-color');
  });
  if (res) {
    console.log('Still at least one field to go!');
  } else {
    console.log('You can submit!');
  }
});

I hope this will helpful to you. 希望对您有帮助。

You can use below code 您可以使用以下代码

$('.formValidation input[type=submit]').on('click',function(e){
  e.preventDefault();

var isValid;
$("input").each(function() {
   var element = $(this);
   if (element.val() == "") {
       isValid = false;
     element.css('border-color','red');
   }else{
isValid = true;
     element.css('border-color','');
}
});
  if (isValid==false) {
    console.log('Still at least one field to go!');
  } else {
    console.log('You can submit!');
  }
});

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

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