2016-09-19 5 views

答えて

0

の場合=のcontentEditable "真" のdivは、あなたが

<div class="md-editor" contenteditable="true"><div class="imustgetthidivbyselecttext" onclick='call(this);'>help</div></div> 

function call(id) { 
     alert(id.className); // For current div 
     alert(id.parentElement.className); // For parent div where contenteditable="true" 
    } 
+0

私はあなたが何か他のものを探しているかどうかを知りましょう –

0

アップデートコードの下に使用することができ、親のdivである:これはChromeで動作します。古いIEのバージョンでは多少異なる場合があります:

window.getSelection().getRangeAt(0).commonAncestorContainer.parentElement; 

フィドル:https://jsfiddle.net/xs36c3a5/

0

function getSelectedText() { 
 
    var text = ''; 
 
    
 
    if (window.getSelection) { 
 
     text = window.getSelection().toString(); 
 
    } 
 
    else if (document.selection && document.selection.type != "Control") { 
 
     text = document.selection.createRange().text; 
 
    } 
 
    
 
    return text; 
 
}
<div contenteditable="true" style="border: solid 1px black; width: 300px;">hello world</div> 
 
<input type="button" onclick="alert(getSelectedText())" value="Get selected text" />

関連する問題