2013-04-28 89 views
0

我有一個非常長的表,我正在使用搜索框來搜索錶行。 .focus()函數集集中在表格行元素上,但立即跳回到搜索框並將焦點設置到它。我不知道爲什麼會發生這種情況。焦點丟失在錶行元素後立即設置

<ice:inputText value="#{bean.searchStr}" maxlength="8" size="8" 
            onkeyup="enterToSearch(event, this.id);"/> 
..... 
<ice:commandLink actionListener="#{bean.search}"></ice:commandLink> 

function focusSelectedRow(rowId) { 
    // Using some other element's id to get to the tr element 
    // and rowId does not correspond to the actual table row 
    var rowElem = document.getElementById(rowId); 
    var trElem = rowElem.parentNode.parentNode.parentNode; 
    var tableElem = trElem.parentNode.parentNode; 
    var children = trElem.parentNode.childNodes; 
    var rowNum = 1; 
    var rowHeight = trElem.offsetHeight; 
    for (var i=0 ; i < children.length ; i++) { 
     if(children[i] == trElem) rowNum = i+1; 
    } 
    trElem.tabIndex = 999; 
    trElem.style.backgroundColor = '#FFFF00'; 
    trElem.focus(); 
    // The below is used because, if the scroll bar is manually adjusted the focus 
    // is not retained for the next search 
    tableElem.scrollTop = rowNum*rowHeight; 
} 

function enterToSearch(event, id) { 
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode; 
    if(keyCode == 13) 
    { 
     document.getElementById(id).parentNode.parentNode.childNodes[3].childNodes[0].click(); 
    } 
} 
+0

你有沒有在搜索框中輸入一個觸發?這個功能的上下文是什麼?我們需要了解更多細節,也許你可以使用JsFiddle。 – fcortes 2013-04-28 23:52:34

+0

請參閱更多代碼,是否有可能獲得JsFiddle? – 2013-04-29 00:43:46

回答

0

的解決方案是增加以下線之後.focus()功能

trElem.scrollToIndex();