나는 href의 목록을 가지고 있으며 그 목록에서 드롭 다운 목록을 만들고 싶습니다.드롭 다운 목록 생성
어떻게 jQuery를 사용하지 않고이 작업을 수행 할 수 있습니까? 나는 자바 스크립트에 새로 온 사람
...
나는 href의 목록을 가지고 있으며 그 목록에서 드롭 다운 목록을 만들고 싶습니다.드롭 다운 목록 생성
어떻게 jQuery를 사용하지 않고이 작업을 수행 할 수 있습니까? 나는 자바 스크립트에 새로 온 사람
...
var select = document.createElement("select");
for (var i=0; i<hrefs.length; i++) {
var link = hrefs[i];
var option = document.createElement("option");
option.value = hrefs[i];
option.innerHTML = i + ": " + hrefs[i]; // this is the label
select.appendChild(option);
}
document.body.appendChild(select);
나는 라벨이 맞다면 확실하지 않다; 그것은 당신이 hrefs의 목록과 레이블로 무엇을하기를 원하는지에 달려 있습니다. 나는 당신이 [ "http://google.com", ...]과 같은 링크를 가지고 있다고 가정하고 있습니다.
자바 스크립트 1.6이있는 경우는 다음과 같이 뭔가를 할 수 있습니다
는var hrefs = ['http://stackoverflow.com', 'http://example.com'],
select = document.createElement("select"),
str = hrefs.map(function(l){
return '<option>'+l+'</option>'
});
select.innerHTML = str.join('');
document.body.appendChild(select);
당신은
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
(MDC를 통해 - https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map)과 .MAP 모방 할 수 BTW
, 편집 할 수 '고객의 요구에 맞게 ''를 반환하십시오. 일반적인 생각은 ... – NilColor