正在尝试将变量传递到window.location.hash

Trying to pass variable to window.location.hash

本文关键字:window location hash 变量      更新时间:2024-04-15

很难实现这一点。

使用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,因此可能实际上并没有附加事件。