2016-03-04 120 views
0

有一個按鈕和一個conableitable div。jQuery從突出顯示的文本中獲取類名

在div中有一些文字。選定或突出顯示的文本位於突出顯示的類中。

編輯: 如果我點擊一個按鈕,我需要跨班級('saymyname')的名稱,因爲這是突出顯示。

例如: HTML:

<button>click</button> 
<div contenteditable="true"> 
    A none highlighted text and now 
    <span class='saymyname'>i'm highlighted</span> 
    more text 
    <span class='im_temporary_not_important'>more text</span> 
</div> 

我怎麼能與jQuery處理呢?

$(button).click(function(){ 
    ? 
}); 
+0

你需要在div內的所有類跨度? –

+1

這不是一個簡單的請求。你將如何處理涵蓋幾個不同元素的多個部分的選擇?我的建議是查看[Rangy插件](https://github.com/timdown/rangy),因爲它至少可以幫助獲取包含文本選擇的元素。 –

+0

只有突出顯示的那個('saymyname') – kuki

回答

0

入住這

$("button").click(function(){ 
 
    $("#content span").each(function(){ 
 
     $(".classNames").append($(this).attr("class")+"<br>"); 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button>click</button> 
 
<div contenteditable="true" id="content"> 
 
    A none highlighted text and now 
 
    <span class='saymyname'>i'm highlighted</span> 
 
    more text 
 
    <span class='im_temporary_not_important'>more text</span> 
 
</div> 
 
<br> 
 
<div class="classNames"> 
 
</div> 
 

 

+0

謝謝@Divyesh Savaliya。但我只需要一個如何通過光標突出顯示(如myname) – kuki

相關問題