Javascript Jquery如何在多个位置运行函数
我对Javascript相当陌生,我正试图编写一些比较两个日期的jquery。如果比较的天数小于或等于一个数字,则我希望将一些类添加到名为Javascript Jquery如何在多个位置运行函数,javascript,jquery,function,loops,Javascript,Jquery,Function,Loops,我对Javascript相当陌生,我正试图编写一些比较两个日期的jquery。如果比较的天数小于或等于一个数字,则我希望将一些类添加到名为.momentum container的父级的LI子元素中 当脚本在一个DIV容器上运行时,它会按预期工作,问题是当我添加其他DIV容器时。我试图调用一个函数来读取.momentum容器的内容并运行脚本,找到下一个.momentum容器,然后执行相同的操作。相反,它看起来只读取第一个容器的内容,并复制结果 在小提琴中你可以看到两排7小节。最上面一行应该有7根钢
.momentum container
的父级的LI
子元素中
当脚本在一个DIV
容器上运行时,它会按预期工作,问题是当我添加其他DIV
容器时。我试图调用一个函数来读取.momentum容器的内容
并运行脚本,找到下一个.momentum容器
,然后执行相同的操作。相反,它看起来只读取第一个容器的内容,并复制结果
在小提琴中你可以看到两排7小节。最上面一行应该有7根钢筋,最下面一行应该只有3根钢筋。取而代之的是两排7
非常感谢您的帮助
//比较两个日期的原型
Date.prototype.DaysBetween=函数(){
var intMilDay=24*60*60*1000;
var intMilDif=参数[0]-此;
var intDays=数学地板(intMilDif/intMilDay);
返回工作日;
};
今天=12+'/'+17+'/'+2014年;
//计算动量的函数
函数calcMomentum(currentPost、olderPost){
var current=$(currentPost.val();
var older=$(olderPost.val();
var todayD=新日期(今天);
var postD=新日期(当前);
var otherD=新日期(旧日期);
//比较日期
var阻力=(日后和日之间(今天));
var移动=(其他日之间(后d));
momomentum变量=(阻力+运动);
//函数使用上述变量在满足条件时应用类
$(“.momentum”)。每个(函数(){
if(momomentum当有多个元素时,应该始终迭代父元素
试试这个:
工作示例
//比较两个日期的原型
Date.prototype.DaysBetween=函数(){
var intMilDay=24*60*60*1000;
var intMilDif=参数[0]-此;
var intDays=数学地板(intMilDif/intMilDay);
返回工作日;
};
今天=12+'/'+17+'/'+2014年;
//计算动量的函数
函数calcMomentum(currentPost、olderPost、父级){
var current=$(currentPost,parent).val();
var older=$(olderPost,parent).val();
var todayD=新日期(今天);
var postD=新日期(当前);
var otherD=新日期(旧日期);
//比较日期
var阻力=(日后和日之间(今天));
var移动=(其他日之间(后d));
momomentum变量=(阻力+运动);
//函数使用上述变量在满足条件时应用类
$(“.momentum”,父级)。每个(函数(){
如果(momomentum)哇,非常感谢您的快速回复,这真是太棒了。
// prototype to compare two dates
Date.prototype.DaysBetween = function () {
var intMilDay = 24 * 60 * 60 * 1000;
var intMilDif = arguments[0] - this;
var intDays = Math.floor(intMilDif / intMilDay);
return intDays;
};
today = 12 + '/' + 17 + '/' + 2014;
// function that calculates the momentum
function calcMomentum(currentPost, olderPost) {
var current = $(currentPost).val();
var older = $(olderPost).val();
var todayD = new Date(today);
var postD = new Date(current);
var otherD = new Date(older);
// compare dates
var resistance = (postD.DaysBetween(todayD));
var movement = (otherD.DaysBetween(postD));
var momomentum = (resistance + movement);
// function uses the above variables to apply classes if condition is met
$(".momentum").each(function () {
if (momomentum <= 6) {
$(this).children("li.two").addClass("level-two");
}
if (momomentum <= 5) {
$(this).children("li.three").addClass("level-three");
}
if (momomentum <= 4) {
$(this).children("li.four").addClass("level-four");
}
if (momomentum <= 3) {
$(this).children("li.five").addClass("level-five");
}
if (momomentum <= 2) {
$(this).children("li.six").addClass("level-six");
}
if (momomentum <= 1) {
$(this).children("li.seven").addClass("level-seven");
}
});
}
$(".momentum-container").each(function () {
calcMomentum(".js-post-date", ".js-older-date");
});
// prototype to compare two dates
Date.prototype.DaysBetween = function () {
var intMilDay = 24 * 60 * 60 * 1000;
var intMilDif = arguments[0] - this;
var intDays = Math.floor(intMilDif / intMilDay);
return intDays;
};
today = 12 + '/' + 17 + '/' + 2014;
// function that calculates the momentum
function calcMomentum(currentPost, olderPost, parent) {
var current = $(currentPost,parent).val();
var older = $(olderPost,parent).val();
var todayD = new Date(today);
var postD = new Date(current);
var otherD = new Date(older);
// compare dates
var resistance = (postD.DaysBetween(todayD));
var movement = (otherD.DaysBetween(postD));
var momomentum = (resistance + movement);
// function uses the above variables to apply classes if condition is met
$(".momentum",parent).each(function () {
if (momomentum <= 6) {
$(this).children("li.two").addClass("level-two");
}
if (momomentum <= 5) {
$(this).children("li.three").addClass("level-three");
}
if (momomentum <= 4) {
$(this).children("li.four").addClass("level-four");
}
if (momomentum <= 3) {
$(this).children("li.five").addClass("level-five");
}
if (momomentum <= 2) {
$(this).children("li.six").addClass("level-six");
}
if (momomentum <= 1) {
$(this).children("li.seven").addClass("level-seven");
}
});
}
$(".momentum-container").each(function () {
calcMomentum(".js-post-date", ".js-older-date", $(this));
});