Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.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-if语句有两个条件,当两个条件都被传递时,它会添加一个类_Javascript_Jquery_If Statement - Fatal编程技术网

Javascript jQuery-if语句有两个条件,当两个条件都被传递时,它会添加一个类

Javascript jQuery-if语句有两个条件,当两个条件都被传递时,它会添加一个类,javascript,jquery,if-statement,Javascript,Jquery,If Statement,我试图写一个带有2个条件的if语句 条件一是查看是否具有类class=“过期” 第二个条件是查看中的日期时间和中的日期时间 HTML <div><time class="overdue" datetime="2013-03-10">March 10th, 2013</time></div> <div><time datetime="2013-03-11">March 11th, 2013</time></d

我试图写一个带有2个条件的if语句

条件一是查看
是否具有类
class=“过期”

第二个条件是查看
中的日期时间和
中的日期时间

HTML

<div><time class="overdue" datetime="2013-03-10">March 10th, 2013</time></div>
<div><time datetime="2013-03-11">March 11th, 2013</time></div>
<a href="#" class="cal-evt"><time datetime="2013-03-10">10</time></a>
<a href="#" class="cal-evt"><time datetime="2013-03-11">11</time></a>
jQuery

if ($("time").hasClass("overdue")) {

    $("a.cal-evt").addClass("red");

}

return false;

我建议,在适当的时候,使用以下命令来代替
if

// find the element with a datetime attribute within an a element of
// class 'cal-evt', filter that collection:
$('a.cal-evt [datetime]').filter(function(){
    // keeping only those whose datetime attribute is equal to the datetime attribute
    // of the (first) 'time.overdue' element:
    return this.getAttribute('datetime') == $('time.overdue').attr('datetime');
// find the closest ancestor a element, and add the class 'red' to it:
}).closest('a').addClass('red');

参考资料:

  • CSS:
  • JavaScript
  • jQuery:

您可以使用jquery的遍历方法,并使用它们来定位所需的元素

$("time.overdue").each(function(){  // for each time that has overdue class
    var time = $(this).attr('datetime'); // store its datetime value

    $("a.cal-evt").filter(function(){ // find all the cal-evt links that contain the same time
        return $('time', this).attr('datetime') == time;
    }).addClass("red"); // apply the red class on the filtered items

});

上演示,可能是这样的:

$( "time" ).filter( "[datetime='" + $( 'time.overdue' ).attr( 'datetime' ) + "']" ).parent( 'a' ).addClass("red");

工作小提琴:


谁曾否决过它,为什么?它可以按要求工作。

听起来你需要使用一个
&&
,读作“and”。例如
if(predicate1和&predicate2){doSomething()}
“如果
中的日期时间和
中的日期时间都匹配”-有2个
,还有2个
,你能说得更具体一些吗..?很抱歉,忘了提到,即使有多个时间标签的类过多,代码也应该起作用谢谢你的帮助。非常可靠的解决方案。很抱歉忘了提到,即使类有多个时间标签过期,代码也应该正常工作
$( "time" ).filter( "[datetime='" + $( 'time.overdue' ).attr( 'datetime' ) + "']" ).parent( 'a' ).addClass("red");
$("time").each(function() {
if($(this).hasClass("overdue")){
    var mydate = $(this).attr("dateTime");

    $(".cal-evt").each(function() {            
        if($(this).find("time").attr("dateTime")==mydate){
              $(this).addClass("red");
        }
    });
}
});
return false;