如何使 href 和 jquery click 事件都起作用
How to make both href and jquery click event work
我有一个报告功能 answerCardInnerLinkReportingCall
,它会在点击特定 div 内的 <a>
标签时被调用。我使用 event.preventDefault();覆盖默认的点击行为。
目前,在使用 window.open('http://whosebug.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>
您可以使用 javascript 的:
window.location.href = 'http://url.here.com';
告诉浏览器导航到一个页面。希望对你有帮助。
其他方法可以是从 answerCardInnerLinkReportingCall 返回 true 或 false 并取决于该调用或不调用 event.PreventDefault();
尝试这样的事情:
$('#answerCard a').click(function(event) {
var loc = $(this).attr('href');
event.preventDefault();
answerCardInnerLinkReportingCall(loc, this);
});
function answerCardInnerLinkReportingCall(loc, that){
// your code to do stuff here
window.open(loc, '_blank');
}
如果您想在脚本执行后使用链接本机功能,我想您不需要使用任何 'event.preventDefault();'。
这样试试
jQuery(document).on('click','#answerCard a', function(event) {
//event.preventDefault();
alert('script running');
answerCardInnerLinkReportingCall(this);
});
还创建了 JS Fiddle。看看吧。
我有一个报告功能 answerCardInnerLinkReportingCall
,它会在点击特定 div 内的 <a>
标签时被调用。我使用 event.preventDefault();覆盖默认的点击行为。
目前,在使用 window.open('http://whosebug.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>
您可以使用 javascript 的:
window.location.href = 'http://url.here.com';
告诉浏览器导航到一个页面。希望对你有帮助。
其他方法可以是从 answerCardInnerLinkReportingCall 返回 true 或 false 并取决于该调用或不调用 event.PreventDefault();
尝试这样的事情:
$('#answerCard a').click(function(event) {
var loc = $(this).attr('href');
event.preventDefault();
answerCardInnerLinkReportingCall(loc, this);
});
function answerCardInnerLinkReportingCall(loc, that){
// your code to do stuff here
window.open(loc, '_blank');
}
如果您想在脚本执行后使用链接本机功能,我想您不需要使用任何 'event.preventDefault();'。
这样试试
jQuery(document).on('click','#answerCard a', function(event) {
//event.preventDefault();
alert('script running');
answerCardInnerLinkReportingCall(this);
});
还创建了 JS Fiddle。看看吧。