javascript - 如何使URL链接到页面的另一种状态?

标签 javascript jquery html

我想在第二个链接处于事件状态时共享页面(例如“http://mypage#green-color”)。

我尝试添加到第二个链接 onClick="window.location='#green-color';" 属性。它更改了浏览器地址窗口中的 URL,但我无法与其他人共享。

code in JS Fiddle

<a href="#" id="all">All Colors</a>
<a href="#green" id="green-color">Green Color</a>
<div id="red"></div>
<div id="green"></div>

页面的两种状态:

$( "#all" ).click(function() {
  $( "#red" ).show(200);
});
$( "#green-color" ).click(function() {
  $( "#red" ).hide(200);
});

最佳答案

尝试在 URL 中传递 divId

像这样

onClick="window.location='#green';"

对于效果,请使用 window.location.hash 方法。

var hashValue=window.location.hash;
if(hashValue=="#green")
{
 $( "#red" ).hide(200);
}

关于javascript - 如何使URL链接到页面的另一种状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19766668/

相关文章:

javascript - 在选择上使用 jQuery 克隆 HTML DIV

html - 带有标题的下拉框 - 不会包含在选项中

html - CSS组合器问题

jquery - 将 div 及其关联元素转换为 canvas jquery?

javascript - Owl Carousel 导航按钮不显示

javascript - 根据 ID 组合多个 jQuery 事件

javascript - Lodash。通过按键智能合并两个对象

jquery - 如何更具体地使用 .slideToggle?

javascript - 当调用函数位于 html 属性中时,无法在 sencha touch 中获取父引用

Javascript 删除对象属性不起作用