简体   繁体   English

为什么我的简单jQuery无法正常工作?

[英]Why isn't my simple jQuery working?

When the button with the class "edit-btn" is clicked I want the element on my page with the class "admin_account_hide" to fade in, and the element with class "admin_account_show" to remove. 单击具有“ edit-btn”类的按钮时,我希望页面上具有“ admin_account_hide”类的元素逐渐淡入,并希望除去具有“ admin_account_show”类的元素。

Here is my HTML: 这是我的HTML:

<form action="" method="post">
<div class="row-fluid admin-product admin-account-show loading-container" id="admin-2">
<div class="span3">admin</div>
<div class="span3">info@mydomain.com</div>
<div class="span3"> </div>
<div class="span3 admin-product-actions">
<button type="button" data-id="2" name="edit" class="btn btn-info edit-btn span6" title="Edit Account" value="Edit">Edit</button>
<button type="button" data-id="2" name="delete" class="btn delete-btn btn-danger span6" title="Delete Account" value="Delete">Delete</button>
</div>
</div>
<div class="row-fluid admin-product loading-container admin-account-hidden" id="admin-2">
<div class="span3"><input type="text" name="eusername" id="eusername" class="span12" value="admin"></div>
<div class="span3"><input type="text" name="eemail" id="eemail" class="span12" value="info@mydomain.com"></div>
<div class="span3"><input type="password" name="epassold" id="epassold" class="span12" placeholder="Old Password"><br>
                                    <input type="password" name="epassnew" id="epassnew" class="span12" placeholder="New Password"></div>
<div class="span3 admin-product-actions">
<button type="submit" data-id="2" name="save" class="btn btn-success edit-product span6" title="Save Changes" value="Save">Save</button>
<button type="button" data-id="2" name="cancel" class="btn delete-btn btn-danger span6" title="Cancel" value="Cancel" onclick="document.location.href='admin-accounts.php';">Cancel</button>
</div>
</div>
</form>

and my jQuery: 和我的jQuery:

$(document).on('click', '.edit-btn', function() {

var $this = $(this);

setTimeout(function() {
    $this.closest('.admin-account-hide').fadeIn(250);
}, 1250);

setTimeout(function() {
    $this.find('.admin-account-show').remove();
}, 1510);

});

EDIT: 编辑:

I have sorted the issue I had with mixing up the underscores with hyphens, that is now not an issue, and I have also tried setting the var $this to reference $(this), but my code still does not work. 我已经解决了将下划线与连字符混在一起的问题,现在这不是问题,并且我还尝试将var $ this设置为引用$(this),但是我的代码仍然无法正常工作。

您没有名为.admin_account_show.admin_account_hide任何类,它们是.admin-account-show.admin-account-hidden

"admin-account-show" is not the same class as "admin_account_show". “ admin-account-show”和“ admin_account_show”不是同一类。 replace your underscores with dashes and test it again. 用破折号替换下划线,然后再次进行测试。

Because this , in the callback you pass to setTimeout , is the window object. 因为在传递给setTimeout的回调中, thiswindow对象。 You should do this : 你应该做这个 :

$(document).on('click', '.edit-btn', function() {
    var $this = $(this);
    setTimeout(function() {
      $this.parent('.admin_account_hide').fadeIn(250);
    }, 1250);

    setTimeout(function() {
      $this.find('.admin_account_show').remove();
    }, 1510);

});

Your queries seem also wrong (where's .admin_account_hide ?) but as you don't provide the whole relevant HTML (don't give more but a minimal example next time) it's hard to tell what would be the correct one. 您的查询似乎也是错误的(.admin_account_hide在哪里?),但是由于您没有提供完整的相关HTML(下次将仅提供一个最小的示例,因此不多提供),因此很难说出正确的方法。

Use .closest() instead of .parent() .Also use correct classnames in jQuery as mentioned in other answers. 使用.closest()而不是.parent() 。还可以在jQuery中使用正确的类名,如其他答案中所述。

Demo Fiddle 演示小提琴

jQuery jQuery的

$(document).on('click', '.edit-btn', function() {
    var $this = $(this);
setTimeout(function() {
    $('.admin-account-hidden').fadeIn(1000);
}, 1250);

setTimeout(function() {
    $this.closest('.admin-account-show').remove();
}, 1510);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM