我正在尝试在我的电子商务上设置折扣监控器,以检查某人是否发布了错误折扣的产品。我的产品页面有大约30个项目,其中一些有一些折扣标签,如下所示:
<span class="nf-off-price"><b>20</b>% off!</span>
<span class="nf-off-price"><b>7</b>% off!</span>
<span class="nf-off-price"><b>12</b>% off!</span>
<span class="nf-off-price"><b>5</b>% off!</span>
<span class="nf-off-price"><b>4</b>% off!</span>
<span class="nf-off-price"><b>4</b>% off!</span>
我需要检查这些标签是否有'nf-off-price&gt; b'等于或高于21,如果是,则发出镀铬警报。我能够实现这一点,但只有在设置一个实数时,才能实现:
if(Notification.permission !== 'granted'){Notification.requestPermission();}
var check = function(){
if ($('.nf-off-price:contains("21")').length > 0) {
n = new Notification( "Discount Alert", {
body: "A possible discount typo was found",
icon : "http://cloudcheckr.com/wp-content/uploads/2015/12/alert.jpg"
});
}
};
setTimeout(check, 5000); // Do not remove. The code needs to fire 5 seconds after page load
据我所知,我的代码会查找一个正好为21的标签,这就是问题所在。如何检查代码中是否至少有一个“.nf-off-price”中的一个包含等于或高于21的那个?
答案 0 :(得分:1)
将检查功能修改为:
var check = function(){
$('.nf-off-price b').each(function() {
if (parseInt(this.innerHTML, 10) > 20) {
n = new Notification( "Discount Alert", {
body: "A possible discount typo was found",
icon : "http://cloudcheckr.com/wp-content/uploads/2015/12/alert.jpg"
});
return;
}
})
};
答案 1 :(得分:1)
if (Notification.permission !== 'granted') {
Notification.requestPermission();
}
var check = function() {
$('.nf-off-price').each(function() {
var price = parseFloat($(this).text());
if (price > 20) {
n = new Notification("Discount Alert", {
body: "A possible discount typo was found " + $(this).text(),
icon: "http://cloudcheckr.com/wp-content/uploads/2015/12/alert.jpg"
});
return false;
}
});
};
setTimeout(check, 5000); // Do not remove. The code needs to fire 5 seconds after page load
只要您的格式与展示格式一样,这将有效。但是,如果你在数字前加上任何字符,parseFloat会给你NaN。
答案 2 :(得分:0)
你可以做到
$(".nf-off-price b").each(value, function() {
if (parseInt(value) > 21) {
// trigger alert here
return // this prevents more than one notification
}
}
我没有测试过这段代码,但我觉得它应该可行