JQuery Ajax在url中添加哈希 [英] JQuery Ajax adding hash in the url

查看:111
本文介绍了JQuery Ajax在url中添加哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this code here that uses struts2-jquery plugin

<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>

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

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

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

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

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

推荐答案

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

Here is a working example (not considering 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在url中添加哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆