2016-07-15 6 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로 태그를 추가 했습니까? 이것은 순수한 자바 스크립트입니다. – vaso123

+0

죄송합니다, 깜빡 아! –

+0

> Mozilla의 또 다른 차이점은 문서가 designMode로 전환되면 해당 문서의 모든 이벤트가 비활성화된다는 것입니다. http://stackoverflow.com/questions/38395353/add-class-active-element-in-contenteditable – zer00ne

답변

1

이 시도 : 여기

은 조각이다

selectedElement.className += " active"; 

그러나 각 메뉴 항목에 itarate, 다른에서 제거하는 것을 잊지 마세요.

관련 문제