在数组中创建包含超链接项的下拉列表
Creating a dropdown list with hyperlinked items in an array
我对JS非常陌生,所以原谅这个问题的简单性…尝试使用数组创建包含超链接项的下拉列表。最终产品将有数百个条目,因此节省空间非常重要。
有两件事要记住:我不能使用JQuery,我必须把它集成到WordPress中。
我知道我这样做是不正确的,但是下面的代码应该给我一个准确的想法,我正在尝试做什么。如有任何帮助,不胜感激。
<body>
<form id="siteList">
<select id="selectSite">
<option>Choose a site</option>
</select>
</form>
</body>
</script>
var myArray = new Array("Google", "Yahoo", "Bing", "Gmail", "Facebook");
var links = new Array("http://www.google.com", "http://www.yahoo.com", "http://www.bing.com", "http://www.gmail.com", "http://www.facebook.com");
// Get dropdown element from DOM
var dropdown = document.getElementById("selectSite")
// Loop through the array
for (var i = 0; i < myArray.length; ++i) {
// Append the element to the end of Array list
dropdown[dropdown.length] = new Option(myArray[i].link(links[i]), myArray[i].link(links[i]));
}
</script>
http://jsfiddle.net/L2awjmvd/您必须编写如下代码:
<script type="text/javascript">
function createarray() {
var myArray = new Array("Google", "Yahoo", "Bing", "Gmail", "Facebook");
var links = new Array("http://www.google.com", "http://www.yahoo.com", "http://www.bing.com", "http://www.gmail.com", "http://www.facebook.com");
var dropdown = document.getElementById("selectSite");
for (var i = 0; i < myArray.length; ++i) {
var option = document.createElement("option");
option.text = myArray[i];
option.value = links[i];
dropdown.add(option);
// dropdown[dropdown.length] = new Option(myArray[i].link(links[i]), myArray[i].link(links[i]));
}
}
function selectSiteRedirect(selectedSite) {
window.location.href = selectedSite;
}
</script>
<body onload="createarray();">
<form id="siteList">
<select id="selectSite" onchange="selectSiteRedirect(this.value);">
<option>Choose a site</option>
</select>
</form>
</body>
我认为这是正确的做法:
<script>
var my_array = ["Google", "Yahoo", "Bing"];
var my_array_links = ["www.google.com", "www.yahoo.com", "www.bing.com"];
var select = document.getElementById("selectSite");
for (i = 0; i < my_array.length; i++) {
var opt = document.createElement('option');
opt.value = my_array[i];
opt.innerHTML = my_array_links[i];
select.appendChild(opt);
}
</script>
这里是http://jsfiddle.net/fvjeunbu/
如果你想在用户选择一个选项时重定向,那么选择声明应该是:
<select id="selectSite" onchange="javascript: window.location.assign(this.value);">
<script>
function pageLoad() {
var my_array = ["Google", "Yahoo", "Bing"];
var my_array_links = ["www.google.com", "www.yahoo.com", "www.bing.com"];
var select = document.getElementById("selectSite");
for (i = 0; i < my_array.length; i++) {
var opt = document.createElement('option');
opt.value = my_array_links[i];
opt.innerHTML = my_array[i];
select.appendChild(opt);
}
}
window.onload = pageLoad;
function selectSiteRedirect(selectedSite) {
// window.location.href = selectedSite;
if(selectedSite!="selected")
window.open(selectedSite, '_blank');
}
</script>
<div>
<select id="selectSite" onchange="selectSiteRedirect(this.value)">
<option selected="selected" value="selected">Choose a site</option>
</select>
</div>
相关文章:
- 使用JavaScript从超链接加载时的默认下拉值
- 如何在ReactJs中链接下拉列表和文本区域
- 当链接单击-jQuery时,第二层下拉列表将隐藏
- 当按下链接以在不同页面上的表单中的下拉列表中设置值时
- 如何使用AngularJS链接下拉列表
- javascript下拉列表-无法添加链接
- 如何通过在Javascript中单击链接,用链接的值设置下拉列表值
- 如何通过单击链接为具有 URL 值的下拉列表设置值
- 链接下拉列表选择列表代码 -- 连接数据
- WordPress 类别下拉菜单选择链接断开
- 如何将选定的下拉列表值附加到aspx页面本身的超链接查询字符串中
- JavaScript:根据下拉菜单更改链接值
- 如何在弹出窗口中的同一页面上用下拉列表中的预选选项创建超链接
- 下拉显示Javascript链接菜单的问题
- 用HTML下拉菜单更改链接
- 在数组中创建包含超链接项的下拉列表
- 当我按下asp.net超链接时,我想在浏览器中打开新选项卡
- Jquery/JS和CSS水平列表导航单击下拉超链接不工作
- 如何在禁用js的情况下处理javascript超链接
- 下拉菜单超链接单击事件问题