本文介绍了hasClass即使切换它也总是返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

<form>
    <input type="text" class="add-new">
    <input type="submit" class="hidden" id="add-new" value="Add new">
<form>

以及带有此代码的js

and a js with this code on

$("#add-new").on('click', function(event){
    event.preventDefault();
    if ($(this).val() === "Add"){
        $(this).val("Cancel");
    }else{
        $(this).val("Add");
    }
    console.log($(".add-new").hasClass('hidden')); // this is always false why??

    $(".add-new").val("").toggleClass('hidden');
    if ($(".add-new").hasClass('hidden') === false) {
        $(".add-new").focus();
    }
});

虽然我总是输入,如果我从不关注输入框。为什么我切换它总是假的?从chorme的命令行工作很好,但焦点仍然无法工作。因为我在我的电脑前已经超过9小时,你看到我的javascript代码有什么问题吗?

Although I always enter the if I never get focus on the input box. Why is it always false since I toggle it? From chorme's command line works great but the focus still won't work. Please since I've been over 9 hours in front of my pc do you see anything wrong with my javascript code?

推荐答案

其中输入你想找到?
此时 console.log($(。add-new)。hasClass('hidden')); 输入类 .add-new 没有类 .hidden

Which input do you want to find?At this point console.log($(".add-new").hasClass('hidden')); input with class .add-new doesn't have class .hidden

$(".add-new").val("").toggleClass('hidden'); // Now it has
if ($(".add-new").hasClass('hidden') === false) { // And it will be always false because now it has .hidden
    $(".add-new").focus();
}

这篇关于hasClass即使切换它也总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 07:58