简体   繁体   English

从href调用JS函数中删除#?

[英]Remove # from href calling JS function?

Hey I have a function which is called from a html link like this: 嘿,我有一个从html链接调用的函数,如下所示:

 <a href='#' onclick='javascript:populate("m")'>My Messages</a><br/>

Function called: 函数调用:

 function populate(q){

    switch(q){
    case 'm':  messages_document(call_data(q+'.php','main')); break;
    }
    return (false);
  }

The problem i have is the url in the browser adds a # to the it - which seems to prevent me clicking the link again unless i remove the hash, is there a way to stop it loading in the browser url ? 我遇到的问题是浏览器中的URL向它添加了一个#-似乎阻止了我再次单击链接,除非我删除了哈希,是否有办法阻止它在浏览器URL中加载?

onclick='return populate("m");'

Load your script in the document head(that is, stuff it between two <head> and <script type="text/javascript"> tags). 将脚本加载到文档头中(即,将其填充在两个<head><script type="text/javascript">标签之间)。 Example : 范例

<head>
    <script type="text/javascript">
        function test(id){
            alert(id);
        }
    </script>
</head>
<a href='#' onclick="test('Test');">My Messages</a>​

I've tested it and it works fine. 我已经对其进行了测试,并且效果很好。

try 尝试

<a href="javascript:;" onclick="populate('m');">My Messages</a>
<script>
function populate(q) {
    switch(q) {
        case 'm':  messages_document(call_data(q + '.php', 'main')); break;
    }
  }
</script>

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

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