简体   繁体   English

为什么此Javascript函数不起作用?

[英]Why doesn't this Javascript function work?

i would like to be able to click a link and have a form show up. 我希望能够单击一个链接并显示一个表格。 ideally with smoothly maybe use delay? 理想情况下可能会使用延迟? so far i click on the link and nothing happens. 到目前为止,我单击链接没有任何反应。

My Javascript 我的Javascript

<script type="text/javascript">
    function showForm() {
        document.getElementById('showme').style.display = "block";
    }
    </script>

my HTML 我的HTML

<a class="non_link" href="" alt="start a new post" onmouseclick="showForm();">
   Start A New Post
</a>
<form action="#" id="showme" method="post">

my CSS 我的CSS

#showme {
    font-size: 0.5em;
    color: #000;
    font-weight: normal;
    margin-top: 20px;
    display: none;
}

took out the # sign and still doesn't work 拿出#号,仍然无法正常工作

# is not part of the id: #不是ID的一部分:

document.getElementById('showme')

Additionally, that should probably be onclick you're using. 此外,可能应该是您使用的onclick Plus, try setting the href to something like href="javascript:void(0);" 另外,尝试将href设置为类似href="javascript:void(0);" (other expressions that return a falsy value should work too afaik). (其他返回虚假值的表达式也可以正常工作)。

Don't use a # sign in getElementById 不要在getElementById使用#

document.getElementById('showme').style.display = "block";

You want the id, not the selector. 您需要的是ID,而不是选择器。 See here . 这里

document.getElementById() doesn't need the # .That's a jquery standard kept in line with how you define css ids document.getElementById()不需要# 。这是一个与您定义CSS ID的方式一致的jquery标准

See MDN: https://developer.mozilla.org/en/DOM/document.getElementById 参见MDN: https//developer.mozilla.org/en/DOM/document.getElementById

Your code is working in below test HTML file: 您的代码正在以下测试HTML文件中运行:

<html>
<head>
<style type="text/css">
#showme {
    font-size: 0.5em;
    color: #000;
    font-weight: normal;
    margin-top: 20px;
    display: none;
}
</style>
<script type="text/javascript">
function showForm() {      
       document.getElementById('showme').style.display='block';    
    }
</script>
</head>
<body>
<a class="non_link" href="#" alt="start a new post" onclick="showForm()">
   Start A New Post
</a>
<form  id="showme" method="">
Content of the Form<br/>
Content of the Form
</form>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM