【问题标题】:Using an anchor href in a javascript onclick在 javascript onclick 中使用锚点 href
【发布时间】:2014-02-18 05:27:22
【问题描述】:

我想在同一个锚点的 onclick 方法中使用一个 href。

这是一个锚点:

<a href="http://www.google.com" onclick="VideoTrackNLog('Best Practices 3','www.google.com');"><img alt="Best Practice 3"      src="timg/BestPractice3.jpg" style="width: 236px; height: 157px" /> </a></td>

这是 onclick 使用的代码:

function VideoTrackNLog(videoname, filename, file) {
        frmVideoTrackNLog.videoname.value = videoname;
        frmVideoTrackNLog.nextpagename.value = filename;
        frmVideoTrackNLog.submit();
        window.open(file,'Video','height=200,width=200,left=190,top=110,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes');
}

想法是将href中的URL移动到函数中的第三个参数。有什么办法可以做到吗?

【问题讨论】:

  • 你不能把href留空,把URL移动成函数参数吗?

标签: javascript html onclick href


【解决方案1】:
VideoTrackNLog('Best Practices 3','www.google.com', this.href);

【讨论】:

  • 是的,但如果你也能解释它会更好。
  • 对不起,我没有意识到这里有很多要解释的。您可以将元素中的任何属性传递到与元素相关的事件中,例如 this.href、this.id 等。哎呀,您甚至可以传递元素本身。
  • 对于不了解 JS 事件处理程序的人来说,解释 this 引用处理程序绑定到的元素会非常有帮助,因为该元素是一个链接,它有一个 @ 987654324@ 包含 URL 的属性。还有一篇很好的文章解释了内联事件处理程序:quirksmode.org/js/events_early.html
【解决方案2】:

href留空,并将 URL 移动为函数参数:

<a href=# onclick="VideoTrackNLog('Best Practices 3','www.google.com', 'http://www.google.com');">
    <img alt="Best Practice 3" src="timg/BestPractice3.jpg" style="width: 236px; height: 157px" />
</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 2013-11-16
    • 2016-10-16
    相关资源
    最近更新 更多