将数组插入 location.href

发布于 2024-12-29 14:27:29 字数 432 浏览 5 评论 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");

I have a program that places input buttons on a form. When a user clicks on the button the onclick attribute uses location.href to direct it to a link.

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

I am trying to store the links in an array.
How can i insert an array into the location call?

Example:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱自由 2025-01-05 14:27:29

只要定义了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];
    // ..
}

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

As long as elementButton_1 is defined, what you posted will work. However, a much better practice is:

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];
    // ..
}

It's better not to define runnable code as a string, for security & maintainability purposes.

心意如水 2025-01-05 14:27:29

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

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

I'm not entirely sure I understand but I think you want:

elementButton_1.setAttribute("onclick", "self.location.href='" + locationArray[0] +"'; return false");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文