Jquery 为什么这两个功能会导致网站其他部分出现故障?
我的网站上的“functions.php”文件中有几个jQuery函数。 我注意到,下面这两个功能虽然工作正常,但会在网站的其他部分造成故障。然而,通过消除它们,这些故障消失了,尽管显然没有任何联系 例如,它们导致jQuery不应用于其他页面,或使按钮切换显示/隐藏不起作用的元素。在我看来,代码似乎是正确的。有什么建议吗Jquery 为什么这两个功能会导致网站其他部分出现故障?,jquery,Jquery,我的网站上的“functions.php”文件中有几个jQuery函数。 我注意到,下面这两个功能虽然工作正常,但会在网站的其他部分造成故障。然而,通过消除它们,这些故障消失了,尽管显然没有任何联系 例如,它们导致jQuery不应用于其他页面,或使按钮切换显示/隐藏不起作用的元素。在我看来,代码似乎是正确的。有什么建议吗 jQuery(function($) { var hieghtThreshold = $('#come-funziona').offset().top -76 -$(w
jQuery(function($) {
var hieghtThreshold = $('#come-funziona').offset().top -76 -$(window).height()/3;
var hieghtThreshold_end = $('#come-funziona').offset().top +$('#come-funziona').height() -76 -$(window).height()/3;
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= hieghtThreshold && scroll <= hieghtThreshold_end ) {
$('.come-funziona-lato').addClass('accendi');
} else {
$('.come-funziona-lato').removeClass('accendi');
}
});
});
对于下面的两行,他说:“uncaughttypeerror:endDate.getDay不是一个函数”
$('#DateMixBeatVoce').html(dayNames[endDate.getDay()] + ' ' + endDate.getDate() + ' ' + monthNames[endDate.getMonth()]);
(代表问题作者发布解决方案,将其从问题移至答案空间)
我不确定这样做是否正确,但至少它是有效的。我只在主页上加载函数
jQuery(function($) {
if ($('body.page-id-5749').length > 0) { // That's home page
var hieghtThreshold = $('#come-funziona').offset().top -76 -$(window).height()/3;
var hieghtThreshold_end = $('#come-funziona').offset().top +$('#come-funziona').height() -76 -$(window).height()/3;
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= hieghtThreshold && scroll <= hieghtThreshold_end ) {
$('.come-funziona-lato').addClass('accendi');
} else {
$('.come-funziona-lato').removeClass('accendi');
}
});
}
});
jQuery(函数($){
如果($('body.page-id-5749').length>0){//这是主页
var hightThreshold=$('#come funziona').offset().top-76-$(window.height()/3;
var hightThreshold_end=$('#come funziona').offset().top+$('#come funziona').height()-76-$(window.height()/3;
$(窗口)。滚动(函数(){
var scroll=$(窗口).scrollTop();
如果(scroll>=HightThreshold&&scroll)选择为html并将其视为整数似乎很奇怪,noOfDaysToAdd=$(“.valMixBeatVoce”).html()
。也许可以尝试.text()
,但我怀疑这是否是问题所在。您可能还想做$.isNumeric(noOfDaysToAdd)
进行验证。注释掉函数的内容。然后开始逐行取消注释,看看是什么打断了页面。浏览器控制台是否显示任何错误?非常感谢各位!我发现了控制台浏览器告诉我的错误。请查看上面帖子中的更新。上面说“未定义的顶部”表示元素不存在在脚本运行时。事实证明,JS中的错误将停止随后的任何JS代码。首先尝试测试元素是否存在,以确保代码的稳定性
var hieghtThreshold_end = $('#come-funziona').offset().top +$('#come-funziona').height() -76 -$(window).height()/3;
$('#DateMixBeatVoce').html(dayNames[endDate.getDay()] + ' ' + endDate.getDate() + ' ' + monthNames[endDate.getMonth()]);
$('#DateMixBeatVoce2').html(dayNames[endDate.getDay()] + ' ' + endDate.getDate() + ' ' + monthNames[endDate.getMonth()]);
jQuery(function($) {
if ($('body.page-id-5749').length > 0) { // That's home page
var hieghtThreshold = $('#come-funziona').offset().top -76 -$(window).height()/3;
var hieghtThreshold_end = $('#come-funziona').offset().top +$('#come-funziona').height() -76 -$(window).height()/3;
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= hieghtThreshold && scroll <= hieghtThreshold_end ) {
$('.come-funziona-lato').addClass('accendi');
} else {
$('.come-funziona-lato').removeClass('accendi');
}
});
}
});