0

我有一个在表单上放置输入按钮的程序。当用户单击按钮时,onclick 属性使用 location.href 将其定向到链接。

elementButton_1.setAttribute("onclick", "self.location.href='http://google.com'; return false");

我正在尝试将链接存储在数组中。如何将数组插入位置调用?

例子:

locationArray = new Array();
locationArray[0] = "http://google.com";

elementButton_1.setAttribute("onclick", "self.location.href=locationArray[0]; return false");
4

2 回答 2

1

只要elementButton_1定义了,您发布的内容就会起作用。但是,更好的做法是:

locationArray = new Array();
locationArray[0] = "http://google.com";

var elementButton_1 = document.getElementById('mybutton');

// define a click handler for the button
elementButton_1.onclick = function() {
    self.location.href = locationArray[0];
    // ..
}

出于安全性和可维护性的目的,最好不要将可运行代码定义为字符串。

于 2012-01-30T19:38:12.127 回答
0

我不完全确定我理解,但我认为你想要:

elementButton_1.setAttribute("onclick", "self.location.href='" + locationArray[0] +"'; return false");
于 2012-01-30T19:32:24.440 回答