2016-07-15 80 views
0

我需要爲此焦點元素添加類。我試圖做的是,檢測哪一行是活動的,而用up/down箭頭鍵導航。
其實我的代碼做了這個功能,但我不知道如何將類添加到這個聚焦的元素。
預先感謝您!在contenteditable中添加類活動元素

這裏是一個片段:

var selectedElement = null; 
 
function setFocus(e) { 
 
    if (selectedElement) 
 
    selectedElement.style.outline = 'none'; 
 

 
    selectedElement = window.getSelection().focusNode.parentNode; 
 
    selectedElement.style.outline = '1px solid #f00'; 
 
}; 
 
document.onkeyup = setFocus; 
 
document.onmouseup = setFocus;
div:focus { outline: none; }
<ul contenteditable="true"> 
 
    <li>The first paragraph</li> 
 
    <li>The second paragraph</li> 
 
</ul>

+0

爲什麼用jQuery標記它?這是純粹的JavaScript。 – vaso123

+0

對不起,我忘了! –

+0

> Mozilla的另一個區別是,一旦文檔切換到designMode,該特定文檔上的所有事件都將被禁用。 http://stackoverflow.com/questions/38395353/add-class-active-element-in-contenteditable – zer00ne

回答

1

試試這個:

selectedElement.className += " active"; 

但是不要忘了itarate每個菜單項,並從其他刪除。