简体   繁体   English

jQuery 数据属性选择器在单击时不起作用

[英]jQuery data-attribute selector doesn't work on click

After click and change data-visible value, I can't select data-visible with new value. click并更改data-visible值后,我不能 select data-visible值具有新值。

JS: JS:

$('#imageUploadAction').on('click', function(e) {
 $('#imageUploadAction').html('Close');
 $('.image-upload-action').toggleClass('h-100 show');
 $('#imageUploadAction').attr('data-visible', 'show');
 $('.image-upload-action-btn').removeClass('d-none');
 $('.image-upload-action-btn').addClass('d-block');
});
$('#imageUploadAction[data-visible="show"]').on('click', function(e) {
 console.log('ok');
});

HTML: HTML:

<div class="image-upload-action d-flex align-items-center flex-column">
  <a id="imageUploadAction" href="javascript:void(0)" data-visible="hide">edit</a>
  <div class="image-upload-action-btn d-flex justify-content-center align-items-center mt-auto d-block">
    data
  </div>
</div>

How can I fix this problem?我该如何解决这个问题?

Demo Here在这里演示

You rather put the value of data-visible in string and check it with a if您宁愿将 data-visible 的值放在字符串中并用 if 检查

 $('#imageUploadAction').on('click', function(e) { let getStatut = $(this).attr('data-visible'); console.clear(); console.log(getStatut); if (getStatut == "hide") { $(this).html('Close').attr('data-visible', 'show'); $(this).parent('.image-upload-action').toggleClass('h-100 show'); $(this).next('.image-upload-action-btn').removeClass('d-none').addClass('d-block'); } else { // show $(this).html('Open').attr('data-visible', 'hide'); $(this).parent('.image-upload-action').toggleClass('h-100 hide'); $(this).next('.image-upload-action-btn').removeClass('d-block').addClass('d-none'); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="image-upload-action d-flex align-items-center flex-column"> <a id="imageUploadAction" href="javascript:void(0)" data-visible="hide">edit</a> <div class="image-upload-action-btn d-flex justify-content-center align-items-center mt-auto d-block"> data </div> </div>

The element does not exist yet该元素尚不存在

At the time you add a click handler for #imageUploadAction[data-visible="show"] no element with this selector exists.在您为#imageUploadAction[data-visible="show"]添加点击处理程序时,不存在具有此选择器的元素。

Use this to bind the click handler to .image-upload-action and any future element with the selector:使用它来将点击处理程序绑定到.image-upload-action以及任何带有选择器的未来元素:

$('.image-upload-action').on('click', '#imageUploadAction[data-visible="show"]', function(e) {
 console.log('ok');
});

See jQuery documentation here:请参阅此处的 jQuery 文档:
https://api.jquery.com/on/#on-events-selector-data https://api.jquery.com/on/#on-events-selector-data

$('#imageUploadAction').on('click', function() {
    if($(this).data('visible') === 'show') {
        console.log('data visible is show');
        $(this).html('Edit').data('visible', 'hide');
    } else {
        console.log('data visible is hide');
        $(this).html('Close').data('visible', 'show');
    }
    $('.image-upload-action').toggleClass('h-100 show');
    $('.image-upload-action-btn').toggleClass('d-block d-none');
});

I'm assuming you are toggling the actions between Edit and Close.我假设您在编辑和关闭之间切换操作。 I have simplified your code and handled data attributes using jQuery data and chained multiple jquery methods on the common selector.我使用jQuery 数据简化了您的代码和处理数据属性,并在公共选择器上链接了多个 jquery 方法。 You can replace this and check in your jsFiddle.您可以替换它并检查您的 jsFiddle。 Multiple click handlers are not ideal to have, so just use one and check the value of the attribute and make decisions based on it.拥有多个点击处理程序并不理想,因此只需使用一个并检查属性的值并根据它做出决定。 You can add else if to add more values other than show and hide.您可以添加 else if 添加除显示和隐藏之外的更多值。

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

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