JQuery的阿贾克斯在URL中添加哈希(JQuery Ajax adding hash in th

2019-07-17 11:14发布

我这里有这个代码,使用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>

当我点击其内容的URL被更改为这样的事情,没有什么改变的URL。 它仍然是相同的,我要的是,当我点击链接这样的事情会发生www.myapp.com/#ajaxvalidation 。 当我运行的代码锚标记被翻译成这样的事情

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

与给定的,我将如何在url中添加一个哈希?

Answer 1:

这里是一个工作示例(不考虑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>


文章来源: JQuery Ajax adding hash in the url