【问题标题】:Further onClick is prevented causing new onclick not to work | Override event.preventDefault()进一步防止 onClick 导致新的 onclick 不起作用 |覆盖 event.preventDefault()
【发布时间】:2017-10-17 20:56:18
【问题描述】:

我正在编写一个脚本,其中原始开发人员可能添加了一个 event.preventDefault() 或一些如何阻止对某些 table td 元素的进一步点击。他们的点击发出了一个 Ajax 请求,我可以在浏览器控制台中看到该请求,但我的新 onClick 处理程序从未执行。我喜欢添加另一个 onclick 处理程序,在其中显示模式弹出窗口。显示模态很容易我只是不确定他们如何停止在元素上添加更多的 onclick 处理。

我已经用以下代码实际测试了代码,看看会发生什么,我可以看到比我收到警报;

Event.prototype.stopPropagation = function(){ alert('stopPropagation') }
Event.prototype.preventDefault = function(e){ console.log (e); alert('preventDefault') }

所以我认为event.preventDefault() 正在被使用。如果阻止了进一步的点击,我如何强制我的代码在点击事件上执行。这是我的代码;

(function (){
    Event.prototype.stopPropagation = function(){ alert('stopPropagation') }
    Event.prototype.preventDefault = function(e){ console.log (e); alert('preventDefault') }
    jQuery('body').on('click', 'td.bookable', function (e){
       console.log ("Clickable!"); //This is not logged.
        //I want to add my show modal popup here.
    });
})();

表格是这样的;

<table class="ui-datepicker-calendar">
   <thead>
      <tr>
         <th scope="col"><span title="Monday">M</span></th>
         <th scope="col"><span title="Tuesday">T</span></th>
         <th scope="col"><span title="Wednesday">W</span></th>
         <th scope="col"><span title="Thursday">T</span></th>
         <th scope="col"><span title="Friday">F</span></th>
         <th scope="col" class="ui-datepicker-week-end"><span title="Saturday">S</span></th>
         <th scope="col" class="ui-datepicker-week-end"><span title="Sunday">S</span></th>
      </tr>
   </thead>
   <tbody>
      ...
      <tr>
         <td class=" ui-datepicker-unselectable ui-state-disabled bookable" title="This date is available"><span class="ui-state-default">9</span></td>
         <td class=" ui-datepicker-unselectable ui-state-disabled not_bookable" title="This date is unavailable"><span class="ui-state-default">10</span></td>
         <td class=" ui-datepicker-unselectable ui-state-disabled not_bookable" title="This date is unavailable"><span class="ui-state-default">11</span></td>
         <td class=" ui-datepicker-unselectable ui-state-disabled not_bookable" title="This date is unavailable"><span class="ui-state-default">12</span></td>
         <td class=" ui-datepicker-unselectable ui-state-disabled not_bookable" title="This date is unavailable"><span class="ui-state-default">13</span></td>
         <td class=" ui-datepicker-week-end ui-datepicker-unselectable ui-state-disabled not_bookable" title="This date is unavailable"><span class="ui-state-default">14</span></td>
         <td class=" ui-datepicker-week-end ui-datepicker-unselectable ui-state-disabled not_bookable" title="This date is unavailable"><span class="ui-state-default">15</span></td>
      </tr>
      <tr>
         <td class=" ui-datepicker-unselectable ui-state-disabled not_bookable" title="This date is unavailable"><span class="ui-state-default">16</span></td>
         <td class=" ui-datepicker-unselectable ui-state-disabled bookable ui-datepicker-today" title="This date is available"><span class="ui-state-default">17</span></td>
         <td class=" ui-datepicker-unselectable ui-state-disabled bookable" title="This date is available"><span class="ui-state-default">18</span></td>
         <td class=" bookable" title="This date is available" data-handler="selectDay" data-event="click" data-month="9" data-year="2017"><a class="ui-state-default" href="#">19</a></td>
         <td class=" bookable" title="This date is available" data-handler="selectDay" data-event="click" data-month="9" data-year="2017"><a class="ui-state-default" href="#">20</a></td>
         <td class=" ui-datepicker-week-end bookable" title="This date is available" data-handler="selectDay" data-event="click" data-month="9" data-year="2017"><a class="ui-state-default" href="#">21</a></td>
         <td class=" ui-datepicker-week-end bookable" title="This date is available" data-handler="selectDay" data-event="click" data-month="9" data-year="2017"><a class="ui-state-default" href="#">22</a></td>
      </tr>
      ...
   </tbody>
</table>

对于具有 19 的 td,当我添加 onClick 回调时它不起作用。我特别不能将 onclick 绑定到 not_bookable 类的元素。

【问题讨论】:

  • 所以调用你的onmousedown。删除 preventDefault 是个坏主意。
  • 谢谢这是一个不错的选择。我想知道是否还有其他方法可以真正克服阻止进一步点击的问题。

标签: javascript jquery html jquery-ui jquery-ui-datepicker


【解决方案1】:

如果您无法删除原始的 preventDefault,我不确定您能否实现此目的。使用另一个事件怎么样,比如mousedown?我并不理想,但是...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 2013-09-29
    相关资源
    最近更新 更多