JavaScript:根据下拉菜单更改链接值
我有一个键值列表,其中键值是人类可读的名称,并且该值应适合链接,例如:JavaScript:根据下拉菜单更改链接值,javascript,url,dictionary,drop-down-menu,Javascript,Url,Dictionary,Drop Down Menu,我有一个键值列表,其中键值是人类可读的名称,并且该值应适合链接,例如: [ {"name": "The home page", "value": "/index.php"}, {"name": "The Pictures Section", "value": "/pics/index.html"}, ... ] 以及链接的关键值列表,例如: [ {"name" : "My Site", "value:":"homesite.com/my_site"}, {"na
[
{"name": "The home page", "value": "/index.php"},
{"name": "The Pictures Section", "value": "/pics/index.html"},
...
]
以及链接的关键值列表,例如:
[
{"name" : "My Site", "value:":"homesite.com/my_site"},
{"name" : "His Site", "value:":"homesite.com/his_site"},
{"name" : "Her Site", "value:":"homesite.com/her_site"},
...
]
我想有一个带有下拉菜单和链接列表的网页。用户将选择一个名称,并相应地附加链接列表。例如,如果用户选择了图片部分,将出现以下列表:
<a href="homesite.com/my_site/pics/index.html"> My Site </a> <br>
<a href="homesite.com/his_site/pics/index.html"> His Site </a> <br>
<a href="homesite.com/her_site/pics/index.html"> Her Site </a> <br>
我该怎么办 下面的代码没有经过测试,我写它只是为了给您一个工作的起点。我希望这就是你想要的:
var arraya = [
{"name": "The home page", "value": "/index.php"},
{"name": "The Pictures Section", "value": "/pics/index.html"},
...
]
var arrayb = [
{"name" : "My Site", "value:":"homesite.com/my_site"},
{"name" : "His Site", "value:":"homesite.com/his_site"},
{"name" : "Her Site", "value:":"homesite.com/her_site"},
...
]
//bind a click event to your a tags
$('a').click(function() {
//get the url of the tag
var url = $(this).attr('href');
loop through arrayb until you match the one you want
var site;
for (site in arrayb)
{
if (site.value == url)
{
break;
}
}
});
//loop through arraya matching on the name of the value matched above
var value;
for(value in arraya)
{
if (site.name == value.name)
{
break;
}
}
//this is your value
site.value;
});
var sections = [
{"name": "The home page", "value": "/index.php"},
{"name": "The Pictures Section", "value": "/pics/index.html"},
...
],
urls = [
{"name" : "My Site", "value:":"homesite.com/my_site"},
{"name" : "His Site", "value:":"homesite.com/his_site"},
{"name" : "Her Site", "value:":"homesite.com/her_site"},
...
];
function generateSelectHtml() {
var html = '<select id="sections" onchange="showSescion(this);">';
for (var i = 0; i < sections.length; i++) {
html += '<option value="' + sections[i].value + '">' + sections[i].name + '</option>';
}
html += "</select>";
return html;
}
function generateSelectDom() {
var el = document.createElement("SELECT");
el.id = "sections";
el.onchange = function() { showSection(el); };
for (var i = 0; i < sections.length; i++) {
var option = document.createElement("option");
option.value = sections[i].value;
option.innerText = sections[i].name;
el.appendChild(option)
}
return el;
}
function showSection(box) {
var container = document.getElementById("listContainer");
for (var i = 0; i < urls.length; i++) {
var item = document.getElementById("a");
item.href = urls[i].value + box.value;
item.innerText = urls[i].name;
container.appendChild(item);
}
}
generateSelectDom方法创建实际的dom元素,因此您可以使用appendChild将其添加到现有dom中
showSection方法只是将所选的值输出到id为listContainer的元素中。没有证据表明使用了jquery,不是在他的代码中,也没有添加任何与jquery相关的标记。如果您在代码中使用jquery,您可能希望至少让他知道,更不用说解释了。任何不使用jquery的人都需要重新发布它不是2005年,欢迎来到革命!是的,你说得对,但现在还早,我还没抽过烟。我不使用jquery,觉得它是个糟糕的库。有更好的,只是不那么受欢迎。但是,嘿,如果你喜欢成为LCD的一部分,那你就要意识到,并不是每个人都像你一样思考。嘿,我在这方面没有任何问题。它得到了很好的支持和理解,如果我死于一场可怕的飞机失事,有人需要理解我写了什么,否则我的公司可能会破产……如果这些是你的观点,那么MooTools和Dojo也符合条件,更不用说基于谷歌的lib/gwt了。
document.getElementById("AN_ID").innerHTML = generateSelectHtml();