簡體   English   中英

刪除/隱藏javascript中的select選項而不使其顯得笨拙(需要延遲select元素的本機onclick事件)

[英]Removing/hiding a select option in javascript without making it look awkward (need to delay the native onclick event of the select element)

碼:
https://dl.dropboxusercontent.com/u/16952797/webdev/uppg1/kontakt.html
http://jsfiddle.net/v8uMJ/ (結果框未成功呈現頁面或重現錯誤)

相關代碼:

function addEvent(element, eventType, theFunction, capture)
{
    if(element.addEventListener)
    {
        element.addEventListener(eventType, theFunction, capture);
    } 
    else if(element.attachEvent)
    {
       element.attachEvent( "on" + eventType, theFunction);
    }
}

function removeDefaultOption(event)
{
    document.getElementById("selectSuggestion").options[0].style.display = "none";      // <-- looks awkward
    //document.getElementById("selectSuggestion").remove(0);                                            // <-- also looks awkward and needs boundaries so that all options don't get removed after each click
    //delay(1000);                                                                                                                  // <-- tried delaying the thread but it didn't work..
    //setInterval(function(){},1000);                                                                                   // <-- tried delaying the thread but it didn't work..
}

function addEventListeners()
{
...
    addEvent(document.getElementById("selectSuggestion"), "click", removeDefaultOption, false);
}

上下文:因此,我想做的就是每當您單擊建議表單中的select元素(瑞典語:Förslag)時,我希望第一個選項(值:-VäljFörslag--)從列表中消失。 問題是選項顯示得“太快”,所以我要么需要a)設置樣式之后才延遲選項的顯示。顯示第一個選項或b)我需要防止單擊select和的默認事件。然后使用我自己的函數覆蓋它,這樣我就可以控制它應該在何時運行(當您單擊select元素時,我不知道運行的本機函數的名稱)。

使用“焦點”事件而不是“單擊”事件。 當焦點對准功能時,這會激發功能的透明度,而不是“等待”完成點擊。

清理后的JS小提琴: http//jsfiddle.net/vhS3p/1/

document.getElementById("selectSuggestion").addEventListener("focus", removeDefaultOption, false);

嘗試刪除所需的選項,如下所示:

var index = 0;
var select =  document.getElementById("selectSuggestion");
select.removeChild(select[index]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM