HTML标签要添加两种href和工作的onClick(HTML tag want to ad

2019-07-19 20:38发布

我想请教一下HTML标签

<a href="www.mysite.com" onClick="javascript.function();">Item</a>

如何使这种标签带有href的onClick工作? (比较喜欢的onClick第一运行然后HREF)

Answer 1:

你已经拥有你所需要的,与未成年人的语法变化:

<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>标签的onclickhref特性是执行onclick ,然后按照href只要onclick不返回false ,取消该事件(或未被阻止事件)



Answer 2:

使用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> 



Answer 3:

采用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>


Answer 4:

要做到这一点使用下面的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>

这里是工作示例 。