我想请教一下HTML标签
<a href="www.mysite.com" onClick="javascript.function();">Item</a>
如何使这种标签带有href和的onClick工作? (比较喜欢的onClick第一运行然后HREF)
我想请教一下HTML标签
<a href="www.mysite.com" onClick="javascript.function();">Item</a>
如何使这种标签带有href和的onClick工作? (比较喜欢的onClick第一运行然后HREF)
你已经拥有你所需要的,与未成年人的语法变化:
<a href="www.mysite.com" onclick="return theFunction();">Item</a>
<script type="text/javascript">
function theFunction () {
// return true or false, depending on whether you want to allow the `href` property to follow through or not
}
</script>
该默认行为<a>
标签的onclick
和href
特性是执行onclick
,然后按照href
只要onclick
不返回false
,取消该事件(或未被阻止事件)
使用jQuery的 。 您需要捕获click
事件,然后去到该网站。
$("#myHref").on('click', function() { alert("inside onclick"); window.location = "http://www.google.com"; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" id="myHref">Click me</a>
采用ng-click
到位onclick
。 和它的那样简单:
<a href="www.mysite.com" ng-click="return theFunction();">Item</a>
<script type="text/javascript">
function theFunction () {
// return true or false, depending on whether you want to allow
// the`href` property to follow through or not
}
</script>
要做到这一点使用下面的HTML:
<a href="www.mysite.com" onclick="make(event)">Item</a>
<script>
function make(e) {
// ... your function code
// e.preventDefault(); // use this to NOT go to href site
}
</script>
这里是工作示例 。