我想让 href 和 onclick 都正常工作,我在这里找到了解决方案: HTML tag <a> want to add both href and onclick working
在 myFunction 中,我发出一个调用 perl 组件的 AJAX 请求。
当我将 Javascript 调用放在 href 中时它工作正常,但在 onclick 中时却不行。不幸的是,jQuery 和我都不允许这个解决方案,所以请你帮忙。
// AJAX Request doesn't work
<a href="www.mysite.com" onclick="myFunction();">Item</a>
<script type="text/javascript">
function myFunction () {
var xmlHttp = getXMLHttpObject();
xmlHttp.open('GET', 'perl.m', true);
return true;
}
</script>
// AJAX Request ok
<a href="javascript: myFunction('www.mysite.com')">Item</a>
<script type="text/javascript">
function myFunction (url) {
var xmlHttp = getXMLHttpObject();
xmlHttp.open('GET', 'perl.m', true);
window.location.href = goto_url;
}
</script>
perl.m:
<%init>
warn "perl.m";
... update ...
</%init>
最佳答案
请仔细阅读教程。它声明您必须像这样输入 javascript(使用 return 关键字)。
<a href="www.mysite.com" onclick="return theFunction();">Item</a>
除此之外,您应该始终输入 URL(例如 http://stackoverflow.com)。
如果这对您有帮助,请告诉我。
关于javascript - 具有 href 和 onclick 以及 AJAX 请求的 HTML 标记 <a>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25197571/