[英]Further onClick is prevented causing new onclick not to work | Override event.preventDefault()
I am working on a script where the original developers may have added a event.preventDefault() or some how prevented any further clicks on certain table td elements. 我正在编写一个脚本,原始开发人员可能在其中添加了event.preventDefault()或某些如何防止对某些表td元素进行进一步单击的脚本。 Their click makes an Ajax request which I can see in the browser console but my new onClick handler is never executed. 他们的点击发出了Ajax请求,我可以在浏览器控制台中看到该请求,但是从未执行过我的新onClick处理程序。 I do like to add another onclick handler where I show a modal popup. 我确实想在显示模式弹出窗口的地方添加另一个onclick处理程序。 Showing the modal is easy I am just not sure how they stopped adding further onclick handling on the element. 显示模态很容易,我只是不确定他们如何停止在元素上添加进一步的onclick处理。
I have actually test the code with the following code to see what happens and I can see than I get the alerts; 我实际上已经使用以下代码测试了代码,以查看发生了什么,并且可以看到收到警报的情况;
Event.prototype.stopPropagation = function(){ alert('stopPropagation') }
Event.prototype.preventDefault = function(e){ console.log (e); alert('preventDefault') }
So I am thinking event.preventDefault()
is being used. 所以我在想使用event.preventDefault()
。 How can I force my code to execute on a click event if further clicks have been prevented. 如果阻止了进一步的单击,如何强制我的代码在click事件上执行。 Here is my code; 这是我的代码;
(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.
});
})();
The table looks like this; 桌子看起来像这样;
<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>
For the td that has 19 it does not work when I add an onClick callback. 对于具有19的td,当我添加onClick回调时它不起作用。 I particularly cannot bind onclick to elements with the not_bookable
class. 我特别不能将onclick绑定到not_bookable
类的元素。
I'm not sure you'll be able to achieve this if you can't remove the original preventDefault. 如果您无法删除原始的preventDefault,我不确定您是否能够实现。 What about using another event, like mousedown? 如何使用另一个事件(例如mousedown)呢? I'ts not ideal, but... 我不太理想,但是...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.