我有举报功能answerCardInnerLinkReportingCall
单击 <a>
时会调用它特定 div 内的标签。我使用 event.preventDefault();覆盖默认的点击行为。
目前,我在使用window.open('http://stackoverflow.com/', '_blank');
发送所有报告参数后将用户重定向到报告功能中的目标URL方法。
jQuery(document).on('click','#answerCard a', function(event) {
event.preventDefault();
answerCardInnerLinkReportingCall(this);
});
如果我在标签中使用 onclick 函数,我会返回 true,并且无需我手动重定向用户就可以使 href 工作,但是是否可以在单击处理程序中执行相同的操作?我无法使用 onclick,因为我无法控制 html 数据。
我想检查是否有更好的方法来实现这个?
编辑1:添加示例 HTML
<div class="answer" style="display: block;">
<div class="well">
<div id="answerCard" answercardid="check_phone_acard">
<h3 id="answerTitle">check your phone</h3>
<div><ol class="answerSteps"><li>Go to <a title="Link opens in a new window" href="https://test.com" target="_blank">Check phone</a>. If prompted, log in.</li></ol></div>
<label id="seeMoreAnswer">Displaying 1 of 1 steps. </label></div>
<!-- Utility Section -->
<div class="util">
<span class="pull-left"><a id="viewFull" href="/test.jsp?sid=52345">View full article ?</a></span>
<span class="pull-right">
</div>
</div>
</div>
</div>
最佳答案
我想你不需要使用任何“event.preventDefault();”如果您想在脚本执行后使用链接 native 功能。
尝试这样
jQuery(document).on('click','#answerCard a', function(event) {
//event.preventDefault();
alert('script running');
answerCardInnerLinkReportingCall(this);
});
还创建了 JS Fiddle 。检查一下。
关于javascript - 如何使 href 和 jquery click 事件同时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29755460/