Javascript 如果/Else jQuery正在破坏我的脚本

Javascript 如果/Else jQuery正在破坏我的脚本,javascript,jquery,twitter-bootstrap,twitter-bootstrap-3,Javascript,Jquery,Twitter Bootstrap,Twitter Bootstrap 3,我试图设置一个if/else语句,如果类存在,则var num将等于100,如果类不存在,则var num将等于55num了解屏幕将偏移多少像素。任何帮助都将被感激,因为Javascript是我的弱点之一 // Add scrollspy to <body> $('body').scrollspy({target: ".navbar", offset: 55}); // Checks to see if navbar is affixed or not if (($(".affix

我试图设置一个if/else语句,如果类存在,则
var num
将等于100,如果类不存在,则
var num
将等于55<然后使用code>num了解屏幕将偏移多少像素。任何帮助都将被感激,因为Javascript是我的弱点之一

// Add scrollspy to <body>
$('body').scrollspy({target: ".navbar", offset: 55});

// Checks to see if navbar is affixed or not
if (($(".affix-top")[0]){) {
  num = 100;
} else {
  num = 55;
}

// Add smooth scrolling on all links inside the navbar
$(".navbar-lower a").on('click', function(event) {

  // Make sure this.hash has a value before overriding default behavior
  if (this.hash !== "") {

    // Prevent default anchor click behavior
    event.preventDefault();

    // Store hash
    var hash = this.hash;

    // Using jQuery's animate() method to add smooth page scroll
    // The optional number (300) specifies the number of milliseconds it takes to scroll to the specified area
    $('html, body').animate({
      scrollTop: $(hash).offset().top - num
    }, 300, function(){

    // Add hash (#) to URL when done scrolling (default click behavior)
      window.location.hash = hash;
    });

  } // End if

});
//将scrollspy添加到
$('body').scrollspy({target:.navbar',offset:55});
//检查导航栏是否已粘贴
如果($(“.affix top”)[0]){){
num=100;
}否则{
num=55;
}
//在导航栏内的所有链接上添加平滑滚动
$(“.navbar lower a”)。在('click',函数(事件){
//在覆盖默认行为之前,请确保this.hash具有值
如果(this.hash!==“”){
//防止默认锚点单击行为
event.preventDefault();
//存储散列
var hash=this.hash;
//使用jQuery的animate()方法添加平滑页面滚动
//可选数字(300)指定滚动到指定区域所需的毫秒数
$('html,body')。设置动画({
scrollTop:$(哈希).offset().top-num
},300,函数(){
//完成滚动后,将哈希(#)添加到URL(默认单击行为)
window.location.hash=散列;
});
}//如果结束,则结束
});

您不需要额外的括号。另外,只需检查
.length
以查看是否存在元素。这是主观的,但在我看来,它更易于阅读

if ( $(".affix-top").length ) {
    num = 100;
} else {
    num = 55;
}

你有一个不属于你的地方

if (($(".affix-top")[0]){) {
应该是

if (($(".affix-top")[0])) {

这很难读

 if (($(".affix-top")[0]){) {
     num = 100;
 } else {
    num = 55;
 }
它可以简化为这样,因为任何匹配的东西都是真的

如果脚本在加载HTML页面的其余部分之前运行,那么这将不起作用

 <script type="application/javascript"> $(".affix-top").length > 0; // false </script>
 <div class="affix-top"></div>
 <script type="application/javascript"> $(".affix-top").length > 0; // true </script>

你检查过你的控制台了吗?…
if($(“.stappy top”)[0]){
@mhodges当我将它粘贴到我的控制台时,它返回
Uncaught SyntaxError:uncounted token{
这是他们告诉你的。你的代码(他们准确复制的)有语法错误。你在“if($(.stappy top”)[0]){中有一个额外的{本节将其更改为如果“($(.affix-top”)[0]),您说Jquery,为什么不直接针对nav并使用.hasClass('affix-top'))。针对一个可能不存在的对象似乎不是一种很好的样式。我尝试过使用它,但是每次它都会打印出第二个数字,即使该类不存在。@Cody Affix有您可以侦听的事件。这可能提供比匹配CSS类更好的功能。让我们看看:
 <script type="application/javascript"> $(".affix-top").length > 0; // false </script>
 <div class="affix-top"></div>
 <script type="application/javascript"> $(".affix-top").length > 0; // true </script>
  jQuery(document).ready(function(){
       var num = $(".affix-top").length ? 100 : 55;
       //....... insert more code here
  });