Javascript 如何使用';如果';在我的jQuery中是否正确?

Javascript 如何使用';如果';在我的jQuery中是否正确?,javascript,jquery,Javascript,Jquery,我有一个带有“单击”事件的函数。触发“click”后,我需要一个条件语句来根据单击的按钮执行一些操作。我添加了一些代码以使其更清晰: ul和a: <ul> <li class="menu first"><a href="link/to/somwhere"></a></li> <li class="menu second"><a href="link/to/somwhere"></a></li&g

我有一个带有“单击”事件的函数。触发“click”后,我需要一个条件语句来根据单击的按钮执行一些操作。我添加了一些代码以使其更清晰:

ul和a:

<ul>
<li class="menu first"><a href="link/to/somwhere"></a></li>
<li class="menu second"><a href="link/to/somwhere"></a></li>
<li class="menu third"><a href="link/to/somwhere"></a></li>
<li class="menu fourth"><a href="link/to/somwhere"></a></li>
</ul>
<a class="logo" href="link/to/somwhere"></a>
我的问题是,当我点击菜单按钮时,它就是不工作。我想让它做点什么,取决于点击了哪个按钮。当我点击“.first”时,它工作,当我点击“.logo”时,它也工作,但第三个“else”被省略

else if ($('.logo a').data('clicked')) { 
并关闭原始的.on()事件:

并关闭原始的.on()事件:

并关闭原始的.on()事件:

并关闭原始的.on()事件:


处理
.data()
的整个过程似乎是一种迂回的方法:

if ($(this).is('.menu a')) {
  // menu button, or ".first a" for just the first one, etc
}
else if ($(this).is('.logo')) {
  // the logo button
}

处理
.data()
的整个过程似乎是一种迂回的方法:

if ($(this).is('.menu a')) {
  // menu button, or ".first a" for just the first one, etc
}
else if ($(this).is('.logo')) {
  // the logo button
}

处理
.data()
的整个过程似乎是一种迂回的方法:

if ($(this).is('.menu a')) {
  // menu button, or ".first a" for just the first one, etc
}
else if ($(this).is('.logo')) {
  // the logo button
}

处理
.data()
的整个过程似乎是一种迂回的方法:

if ($(this).is('.menu a')) {
  // menu button, or ".first a" for just the first one, etc
}
else if ($(this).is('.logo')) {
  // the logo button
}

如果您试图根据使用数据元素单击的内容进行操作,那么您就遇到了麻烦。您正在使用ajax加载页面,而没有惊人地重置数据-如果您单击第一个,然后单击第二个,那么第一个页面仍然可能有数据('clicked')true

加上已经指出的缺少的括号

建议改为:

$('.menu, .logo').on('click','a',function(e){
        var href = $(this).attr('href');
        if ( $(this).hasClass('first') ) { 
        // do things reserved only for .first button
        }
        else if( $(this).hasClass('logo') ) { 
        // do things reserve only for .logo link
        }
        else { 
        // do things reserved for every other buttons 
        }
        goTo(href);
}

如果您试图根据使用数据元素单击的内容进行操作,那么您就遇到了麻烦。您正在使用ajax加载页面,而没有惊人地重置数据-如果您单击第一个,然后单击第二个,那么第一个页面仍然可能有数据('clicked')true

加上已经指出的缺少的括号

建议改为:

$('.menu, .logo').on('click','a',function(e){
        var href = $(this).attr('href');
        if ( $(this).hasClass('first') ) { 
        // do things reserved only for .first button
        }
        else if( $(this).hasClass('logo') ) { 
        // do things reserve only for .logo link
        }
        else { 
        // do things reserved for every other buttons 
        }
        goTo(href);
}

如果您试图根据使用数据元素单击的内容进行操作,那么您就遇到了麻烦。您正在使用ajax加载页面,而没有惊人地重置数据-如果您单击第一个,然后单击第二个,那么第一个页面仍然可能有数据('clicked')true

加上已经指出的缺少的括号

建议改为:

$('.menu, .logo').on('click','a',function(e){
        var href = $(this).attr('href');
        if ( $(this).hasClass('first') ) { 
        // do things reserved only for .first button
        }
        else if( $(this).hasClass('logo') ) { 
        // do things reserve only for .logo link
        }
        else { 
        // do things reserved for every other buttons 
        }
        goTo(href);
}

如果您试图根据使用数据元素单击的内容进行操作,那么您就遇到了麻烦。您正在使用ajax加载页面,而没有惊人地重置数据-如果您单击第一个,然后单击第二个,那么第一个页面仍然可能有数据('clicked')true

加上已经指出的缺少的括号

建议改为:

$('.menu, .logo').on('click','a',function(e){
        var href = $(this).attr('href');
        if ( $(this).hasClass('first') ) { 
        // do things reserved only for .first button
        }
        else if( $(this).hasClass('logo') ) { 
        // do things reserve only for .logo link
        }
        else { 
        // do things reserved for every other buttons 
        }
        goTo(href);
}
重写代码:

之前将数据设置为true时,不确定为什么要检查数据。现在对我来说毫无意义,也许你想检查一下用户以前是否已经点击过它

// declare outside event as it is not clear from your provided code where this belongs
function goTo(href) {
    $.ajax({ url: href }).done(function(){
        // some code
    });
}

$('.menu, .logo').on('click','a',function(ev){
    var el = $(this), // let's do this once
        hasClicked = el.data('clicked'), // let's do this once
        parent = el.parent(),
        href = el.prop('href'); // use .prop() when dealing with actual properties

    ev.preventDefault(); // prevent the link from it's intentional behaviour

    // why mess with data here? Do it after you've done whatever it is you have to do or it doesn't makes sense you're gonna check it?
    //el.data('clicked', true);

    // use !hasClicked to reverse the condition(s)
    if (parent.hasClass('first') && hasClicked) {
        // do things reserved only for .first button
    } else if (parent.hasClass('.logo') && hasClicked) { 
        // do things reserve only for .logo link
    } else { 
        // do things reserved for every other buttons 
    }

    // wild guess this belongs here
    el.data('clicked', true);

    goTo(href);

});
重写代码:

之前将数据设置为true时,不确定为什么要检查数据。现在对我来说毫无意义,也许你想检查一下用户以前是否已经点击过它

// declare outside event as it is not clear from your provided code where this belongs
function goTo(href) {
    $.ajax({ url: href }).done(function(){
        // some code
    });
}

$('.menu, .logo').on('click','a',function(ev){
    var el = $(this), // let's do this once
        hasClicked = el.data('clicked'), // let's do this once
        parent = el.parent(),
        href = el.prop('href'); // use .prop() when dealing with actual properties

    ev.preventDefault(); // prevent the link from it's intentional behaviour

    // why mess with data here? Do it after you've done whatever it is you have to do or it doesn't makes sense you're gonna check it?
    //el.data('clicked', true);

    // use !hasClicked to reverse the condition(s)
    if (parent.hasClass('first') && hasClicked) {
        // do things reserved only for .first button
    } else if (parent.hasClass('.logo') && hasClicked) { 
        // do things reserve only for .logo link
    } else { 
        // do things reserved for every other buttons 
    }

    // wild guess this belongs here
    el.data('clicked', true);

    goTo(href);

});
重写代码:

之前将数据设置为true时,不确定为什么要检查数据。现在对我来说毫无意义,也许你想检查一下用户以前是否已经点击过它

// declare outside event as it is not clear from your provided code where this belongs
function goTo(href) {
    $.ajax({ url: href }).done(function(){
        // some code
    });
}

$('.menu, .logo').on('click','a',function(ev){
    var el = $(this), // let's do this once
        hasClicked = el.data('clicked'), // let's do this once
        parent = el.parent(),
        href = el.prop('href'); // use .prop() when dealing with actual properties

    ev.preventDefault(); // prevent the link from it's intentional behaviour

    // why mess with data here? Do it after you've done whatever it is you have to do or it doesn't makes sense you're gonna check it?
    //el.data('clicked', true);

    // use !hasClicked to reverse the condition(s)
    if (parent.hasClass('first') && hasClicked) {
        // do things reserved only for .first button
    } else if (parent.hasClass('.logo') && hasClicked) { 
        // do things reserve only for .logo link
    } else { 
        // do things reserved for every other buttons 
    }

    // wild guess this belongs here
    el.data('clicked', true);

    goTo(href);

});
重写代码:

之前将数据设置为true时,不确定为什么要检查数据。现在对我来说毫无意义,也许你想检查一下用户以前是否已经点击过它

// declare outside event as it is not clear from your provided code where this belongs
function goTo(href) {
    $.ajax({ url: href }).done(function(){
        // some code
    });
}

$('.menu, .logo').on('click','a',function(ev){
    var el = $(this), // let's do this once
        hasClicked = el.data('clicked'), // let's do this once
        parent = el.parent(),
        href = el.prop('href'); // use .prop() when dealing with actual properties

    ev.preventDefault(); // prevent the link from it's intentional behaviour

    // why mess with data here? Do it after you've done whatever it is you have to do or it doesn't makes sense you're gonna check it?
    //el.data('clicked', true);

    // use !hasClicked to reverse the condition(s)
    if (parent.hasClass('first') && hasClicked) {
        // do things reserved only for .first button
    } else if (parent.hasClass('.logo') && hasClicked) { 
        // do things reserve only for .logo link
    } else { 
        // do things reserved for every other buttons 
    }

    // wild guess this belongs here
    el.data('clicked', true);

    goTo(href);

});

您缺少
之后的
if
。jQuery是一个用JavaScript编写的库。“else-if”和所有其他条件语句都可以使用它。您发布的代码在语法上不正确。一种更简单的方法是使用
if($(this).is('.logo'))
if($(this).is('.menu a'))
。没有人说else-if在jQuery中无效。jQuery是javascript上的alibrary-buult-它为javascript添加了一组额外的函数。else-if是基本javascript,因此它可以与jQuery结合使用,但它不需要jQuery,在使用jQuery时与使用普通javascript时没有任何区别
之后的
if
.jQuery是一个用JavaScript编写的库。“else-if”和所有其他条件语句都可以使用它。您发布的代码在语法上不正确。一种更简单的方法是使用
if($(this).is('.logo'))
if($(this).is('.menu a'))
。没有人说else-if在jQuery中无效。jQuery是javascript上的alibrary-buult-它为javascript添加了一组额外的函数。else-if是基本javascript,因此它可以与jQuery结合使用,但它不需要jQuery,在使用jQuery时与使用普通javascript时没有任何区别
之后的
if
.jQuery是一个用JavaScript编写的库。“else-if”和所有其他条件语句都可以使用它。您发布的代码在语法上不正确。一种更简单的方法是使用
if($(this).is('.logo'))
if($(this).is('.menu a'))
。没有人说else-if在jQuery中无效。jQuery是javascript上的alibrary-buult-它为javascript添加了一组额外的函数。else-if是基本javascript,因此它可以与jQuery结合使用,但它不需要jQuery,在使用jQuery时与使用普通javascript时没有任何区别
之后的
if
.jQuery是一个用JavaScript编写的库。“else-if”和所有其他条件语句都可以使用它。您发布的代码在语法上不正确。一种更简单的方法是使用
if($(this).is('.logo'))
if($(this).is('.menu a'))
。没有人说else if在jQuery中无效。jQuery是javascript上的alibrary buult-它向javascript添加了一组额外的函数。else if是基本javascript,所以