Javascript JQuery Ajax 在 url 中添加哈希

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13986231/
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-08-24 15:20:54  来源:igfitidea点击:

JQuery Ajax adding hash in the url

javascriptjqueryajaxurlstruts2

提问by KyelJmD

I have this code here that uses struts2-jquery plugin

我这里有使用 struts2-jquery 插件的代码

<h4>Choose A task</h4>
    <ul>
        <s:url value="views/ajaxvalidation.jsp" var="ajaxvalidation" >
            <s:param name="menuId" value="1"/>
        </s:url>
        <li><sj:a targets="resultContent" href="%{ajaxvalidation}">Ajax Validation</sj:a></li>
    </ul>

When I click its content the url is changing to something like this, nothing is changing in the url. it still remains the same, what I want is that when I click the link something like this would happen www.myapp.com/#ajaxvalidation. When I run the code anchor tag is translated to something like this

当我点击它的内容时,网址会变成这样,网址中没有任何变化。它仍然保持不变,我想要的是当我点击链接时会发生这样的事情www.myapp.com/#ajaxvalidation。当我运行代码锚标记被翻译成这样的

<a id="anchor_1365013162" href="javascript:void(0)">Ajax Validation</a>

With that given, how would I add a hash in the url?

有了这个,我将如何在 url 中添加哈希?

回答by Moritz Petersen

Here is a working example (not considering Struts2):

这是一个工作示例(不考虑 Struts2):

<html>
<body>
<a id="123" href="">Add Hash</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $("a").click(function(e) {
        window.location.hash = $(this).attr("id");
        e.preventDefault();
    });
});
</script>
</body>
</html>