从超链接调用 Javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17774731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:34:26  来源:igfitidea点击:

Calling Javascript Function from Hyperlink

javascriptfunctionhyperlink

提问by Matthew Croft

I am working on a page that contains a digital phone directory. The names and numbers are listed, but I would like for more details about the entry to display in a popup when the name is clicked. Unfortunately, I am having a problem with the Javascript. I am very new to this language, but I think that either the code within the function I declared is wrong, the syntax of the argument I am passing is incorrect, or I omitted an essential part of the code. Any assistance you can provide on this issue would be greatly appreciated.

我正在处理一个包含数字电话目录的页面。列出了名称和数字,但我想了解有关单击名称时在弹出窗口中显示的条目的更多详细信息。不幸的是,我在使用 Javascript 时遇到了问题。我对这种语言很陌生,但我认为我声明的函数中的代码是错误的,我传递的参数的语法不正确,或者我省略了代码的重要部分。您可以在此问题上提供的任何帮助将不胜感激。

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script>
Function DisplayContactDetail(contact, windowname) {
    window.open(contact, windowname);
}
</script>
<title>Cigna Security & Reception Phone Directory</title>
</head>
<body>
    <div class="centerelement">
        <div id="indexpageheader">
            CIGNA SECURITY & RECEPTION DIGITAL PHONE DIRECTORY
        </div>
    </div>
    <table>
        <th colspan=2 width="auto">
            ALLIEDBARTON
        </th>
        <tr>
            <td width="50%">
                <a href="" onclick="DisplayContactDetail('cigna.com','ContactDetail')">
                    NEPA District Office
                </a>
            </td>
            <td>(610) 954-8590</td>
        </tr>
        <tr>
            <td width="50%">Post Watch</td>
            <td>(800) 643-8714</td>
        </tr>
    </table>

Thanks in advance!

提前致谢!

回答by Teemu

You've a quoting error in the value of the onclickattribute:

您在onclick属性值中有引用错误:

<a href="" onclick="DisplayContactDetail("http://www.cigna.com")">

Should be for example:

应该是例如:

<a href="" onclick="DisplayContactDetail('http://www.cigna.com')">

Also Functionshould be function, JS is case-sensitive.

Function应该是function,JS 是区分大小写的。