Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery:检查元素是否具有特定样式(非内联)_Javascript_Jquery_Html_Css - Fatal编程技术网

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

Javascript jQuery:检查元素是否具有特定样式(非内联),javascript,jquery,html,css,Javascript,Jquery,Html,Css,是否可以通过jQuery或vanilla JS检查元素是否具有特定的样式 在我的例子中,我想检查页面上的任何输入字段是否有红色边框-通过外部CSS文件应用。没有内联css和样式-attr可用,样式完全是外部的 对于我的初始测试,我从这个StackOverflow答案中得到了以下代码: …但是.css只能测试内联样式 更新 我无法更改HTML,标记必须保持“原样”。红色边框仅通过css。如下所示:创建一个红色的类 $('.formValidation input[type=submit]').o

是否可以通过jQuery或vanilla JS检查元素是否具有特定的样式

在我的例子中,我想检查页面上的任何输入字段是否有红色边框-通过外部CSS文件应用。没有内联css和
样式
-attr可用,样式完全是外部的

对于我的初始测试,我从这个StackOverflow答案中得到了以下代码:

…但是.css只能测试内联样式

更新
我无法更改HTML,标记必须保持“原样”。红色边框仅通过css。如下所示:

创建一个红色的类
 $('.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!');
        }
    });


我希望这将对您有所帮助。

创建一个红色的one类 像

我希望这将对您有所帮助。

您可以使用

getComputedStyle()方法在应用活动样式表并解析这些值可能包含的任何基本计算之后,给出元素的所有CSS属性的值

例如:

var inp=document.getElementsByTagName('input')[0];
var style=window.getComputedStyle(inp,null);
console.log(style.getPropertyValue(“边框颜色”))
input[type='text']{
边框:1px实心rgb(255,0,0);
}
您可以使用

getComputedStyle()方法在应用活动样式表并解析这些值可能包含的任何基本计算之后,给出元素的所有CSS属性的值

例如:

var inp=document.getElementsByTagName('input')[0];
var style=window.getComputedStyle(inp,null);
console.log(style.getPropertyValue(“边框颜色”))
input[type='text']{
边框:1px实心rgb(255,0,0);
}

您可以使用以下代码

$('.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!');
  }
});

您可以使用下面的代码

$('.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!');
  }
});
试着这样做:

 $('.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!');
        }
    });
试着这样做:

 $('.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!');
        }
    });

我无法添加类。HTML标记必须保持“原样”。红色边框仅通过CSS提供。看看更新的问题。但是thx.我不是检查颜色,而是检查它的有效性,我更新了你的JSFIDLE请看那个。我无法添加类。HTML标记必须保持“原样”。红色边框仅通过CSS提供。看看更新的问题。但是thx.我不是检查颜色,而是检查它的有效性,我更新了你的JSFIDLE请看那个。将第一个if-else的两个分支中的isValid变量设置为false。它永远不会成为真的。那是错的。谢谢你纠正我@mizechYou在第一个if-else的两个分支中将isValid变量设置为false。这永远不会成为现实。那是错的。谢谢你纠正我@mizech