尝试将变量传递给window.location.hash

时间:2013-12-07 17:49:04

标签: javascript 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'”nothign发生 “window.location.hash ='+ link'”在网址中使用“+ link”时会出错。

如果我直接输入链接,它可以正常工作:例如“window.location.hash ='#slowshutter'”

我知道链接已正确存储在“text2”中,因为我测试了element2.value而不是“text”,它提出了“#slowshutter”而不是标题......

有什么想法吗?

诺尔

1 个答案:

答案 0 :(得分:0)

你可能想要

element2.onclick=function(){ window.location.hash = link; };

http://www.w3schools.com/jsref/event_onclick.asp

由于您使用的是属性api,因此您可能实际上并未附加该事件。