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

Javascript 正在尝试将变量传递到window.location.hash,javascript,hash,Javascript,Hash,要让它工作起来有些困难 使用javascript字典创建赋值列表,有两个元素:text(标题)和text2(散列锚链接,ex-#slowshutter) 但我似乎无法让链接起作用 var cell2 = row.insertCell(1); var element2 = document.createElement("input"); element2.type = "viewButton"; element2.value = todoDicti

要让它工作起来有些困难

使用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.value而不是“text”,并且它使用了“#slowshutter”而不是标题

有什么想法吗

诺尔

你可能想要

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


由于您使用的是属性api,因此可能没有实际附加事件。

“window.location.hash='+link+”“
element2.onclick=function(){window.location.hash=link};
如果您不介意的话,最后一项。在列表中,当我单击项目标题时,它会在转到链接之前暂时打开键盘。我认为这是因为它是一个“输入”项目。我尝试了“标签”和“文本区域”,但标题没有显示。有没有办法使它成为文本而不是文本输入框?