如何在Anchor标记(<a>)中运行onClick和href

How to run onClick and href both in an Anchor tag (&lt;a&gt;)

本文关键字:运行 onClick href gt 标记 Anchor lt      更新时间:2023-09-26

单击执行href而不是javascript函数时,请帮助我解决这个问题。提前感谢。。。

单击链接时,它正在执行href,但不执行javascript。

有什么想法可以实现吗?

我的代码是:

<div class="ui-grid-a">
  <div class="ui-block-a">
    <img id="first-image-{{this.id}}" src="./img/image-bg.png">
    <div style="text-align: center;">
      <a id="first-camera-{{this.id}}" onclick=" javascript:currentImgViewId()" href="#dialogPage" data-transition="pop"><img src="./img/camera.png"></a>
    </div>
  </div>

点击即为:

function currentImgViewId() {
  alert("hahs");
}

替换此:

onclick="javascript:currentImgViewId()"

通过这个:

onclick="currentImgViewId()"

这是JSFiddle演示

javascript:旨在用作地址栏中的链接/键入,以便浏览器将以下代码作为与页面相关的javascript代码执行。

您需要从currentImgViewId() 中选择return true

function currentImgViewId() (){
  alert('blah');
  return true;
}

return true在您的功能返回后继续处理href,并转到正确的锚点

return false表示不会处理

如果您不返回任何内容,它将返回undefined