正在尝试将变量传递到window.location.hash
Trying to pass variable to window.location.hash
很难实现这一点。
使用javascript字典创建分配列表,有两个元素:text(标题)和text2(散列锚链接,ex-#slowshutter)但我似乎找不到工作的链接。
var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
element2.type = "viewButton";
element2.value = todoDictionary["text"];
var link= todoDictionary["text2"];
element2.id = rowID;
element2.setAttribute("onclick","window.location.hash = link");
element2.className = "viewButton";
cell2.appendChild(element2);
我已经尝试了各种其他方法,我发现它在这里显示在堆栈溢出上,但它们似乎不起作用,因为什么都没发生。
示例"window.location.hash='link'"未出现任何符号"window.location.hash='+link'"在url中使用"+link"时出错。
如果我直接把链接放进去,它会很好地工作:例如"window.location.hash='#slowshutter'"
我知道链接正确地存储在"text2"中,因为我测试了element2.值而不是"text",它出现了"#slowshutter"而不是标题。。。
有什么想法吗?
Noel
您可能想要
element2.onclick=function(){ window.location.hash = link; };
http://www.w3schools.com/jsref/event_onclick.asp
由于您使用的是属性api,因此可能实际上并没有附加事件。
相关文章:
- window.location替换并传递URL历史记录条目中的变量
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- window.location使用jquery mobile实现chrome跳转
- Windows.Open 在 window.Location 工作时不起作用
- 如何将所有作业自定义到window.location.href
- window.location未刷新到现有URL
- 如果window.location.href.indexOf('player=1')添加样式
- 在$state.go之后执行$window.location.reload(true)
- javascript window.location在检查firebug时给了我一个错误的url路径
- angularjs 中 $window.location.reload() 和 $route.reload() 之间的区
- 在AJAX中使用window.location.replace'成功'回调
- IE中不支持JQuery window.location.hash
- PhoneGap如何在更改window.location/document.location后保持Javascript的
- 如何在更改window.location后停止执行
- 警报中的window.location必须重定向到上一页
- 如何在窗体操作或window.location(PHP)中使用数据切换
- 正在将目标添加到window.location
- window.location.htm无法在chrome扩展中工作
- 可以'当window.location.href时,无法从javascript获取post值
- 基于AJAX的网站中的JS window.location