我正在使用这段代码来定位页面上的特定按钮
jQuery('button').on('click', function() {
const num = this.dataset.xToggleable.match(/\d+$/);
if (num) {
jQuery('div[id^=privsec]').hide();
jQuery(`div#privsec-${num}`).show();
}
});这些按钮具有以下标记:
<button class="x-active" role="tab" aria-selected="true" aria-controls="panel-6" data-x-toggle="tab" data-x-toggleable="tab-item-6" data-x-toggle-group="tab-group-e572-5"></button>然而,当我点击其他没有设置aria属性的按钮时,我得到了这个错误。
Uncaught TypeError: Cannot read property 'match' of undefined
at HTMLButtonElement.<anonymous> ((index):1508)
at HTMLButtonElement.dispatch (jquery.js?ver=1.12.4-wp:3)
at HTMLButtonElement.r.handle我如何修改我的代码来避免这种情况?
在页面上的弹出窗口中单击此按钮时出现错误:
<button type="button" class="pum-close popmake-close" aria-label="Close">×</button>发布于 2020-04-25 03:25:02
您的逻辑要求按钮具有data-x-toggleable属性。如果有些按钮上没有该属性,则没有理由在这些按钮上添加事件处理程序。您可以使用属性选择器仅选择具有该属性的元素,例如:
jQuery('button[data-x-toggleable]')https://stackoverflow.com/questions/61415820
复制相似问题