2015-04-04 50 views
1

我的div的列表看起來象下面這樣:如何在DIV中的文本突出顯示時獲取DIV內的Span值? (JavaScript)的

<div id="main1"> 
    <span class="username">Username_1</span> 
    white some text content inside div... 
    blue some text content inside div... 
    yellow some text content inside div... 
</div> 

<div id="main2"> 
    <span class="username">Username_2</span> 
    another test1 text content inside div... 
    another test2 text content inside div... 
    another text test3 content inside div... 
</div> 

當用戶高亮顯示這些div的裏面的一些文本(例如,他強調:「藍色一些文本」從MAIN1格或'test2 text con'from main2) - 如何從<span class="username>{username}</span>獲取用戶名的值?

換句話說,當用戶從MAIN1 DIV突出一些文字,我需要得到的值:USERNAME_1和時,他強調了MAIN2專區內一些文字,我應該得到的值:USERNAME_2等

(如果更簡單,我也可以將ID添加到跨度)。我只能使用純Javascript(而不是jQuery)。謝謝。

+2

你確定你不只是想聽那些DIV上的點擊事件嗎?爲什麼選擇,具體?什麼是阻止用戶同時突出顯示兩個div的文本? – brianvaughn 2015-04-04 16:06:48

+0

可能的重複:http://stackoverflow.com/questions/3731328/on-text-highlight-event – Bladepianist 2015-04-04 16:06:59

+0

用戶選擇一些div內的文本,因爲他想引用這個特定的文本(我有一個不同的JS函數引用) 。 IE瀏覽器。他選擇一些文本並點擊一個按鈕以獲取一個文本表單(並將該選定的文本粘貼到文本表單中)。但我更喜歡創建一個按鈕(當前每個按鈕分別靠近div)。 – NonCoder 2015-04-04 16:09:27

回答

2

您需要獲取所選內容的父元素,然後查看父級的children以查找第一個span元素。該元素的innerHTML將包含您需要的文本。

借用Get parent element of a selected textGet the Highlighted/Selected text 「獲得選擇」 和 「查找選擇的父」 代碼:

window.getSelection().toString() 

function checksel() { 
 
    var txt = getSelectionText(); 
 
    var parent = getSelectionParentElement(); 
 
    var user = null; 
 

 
    if (txt && parent) 
 
    for (var i = 0; i < parent.children.length; ++i) { 
 
     var kid = parent.children[i]; 
 

 
     if (kid.tagName.toLowerCase() == "span") { 
 
     user = kid.innerHTML; 
 
     } 
 
    } 
 

 
    var p = document.createElement('p'); 
 

 
    if (user) { 
 
    p.innerHTML = "user: '" + user + "'. Text: '" + txt + "'."; 
 
    } else { 
 
    p.innerHTML = "No user or no selection."; 
 
    } 
 

 
    document.body.appendChild(p); 
 
} 
 

 
function getSelectionParentElement() { 
 
    var parentEl = null, 
 
    sel; 
 
    if (window.getSelection) { 
 
    sel = window.getSelection(); 
 
    if (sel.rangeCount) { 
 
     parentEl = sel.getRangeAt(0).commonAncestorContainer; 
 
     if (parentEl.nodeType != 1) { 
 
     parentEl = parentEl.parentNode; 
 
     } 
 
    } 
 
    } else if ((sel = document.selection) && sel.type != "Control") { 
 
    parentEl = sel.createRange().parentElement(); 
 
    } 
 
    return parentEl; 
 
} 
 

 
function getSelectionText() { 
 
    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 id="main1"> 
 
    <span class="username">Username_1</span> 
 
    white some text content inside div... blue some text content inside div... yellow some text content inside div... 
 
</div> 
 

 
<div id="main2"> 
 
    <span class="username">Username_2</span> 
 
    another test1 text content inside div... another test2 text content inside div... another text test3 content inside div... 
 
</div> 
 

 
<button id="check" onclick="checksel()">check</button>

1

來獲取文本的頁面中選擇我會讓你自己決定如何使用,因爲坦率地說,我沒有靈感那個。但我懷疑你可能通過將結果存儲在var中並將其傳遞給你的textform來使用它:)。

我嘗試:http://jsfiddle.net/Bladepianist/Lj4x0xks/

+0

你能檢查它是否工作?選擇文字後沒有提示。 – NonCoder 2015-04-04 16:47:31

+0

我修改了它。希望它適合你:)。 編輯:我確實把腳本放在HTML中,因爲用jsfiddle,它被檢測到。應該工作,如果你使用一個單獨的js文件;) – Bladepianist 2015-04-04 17:24:28

+0

仍然沒有警報 - 也許jsfiddle鏈接是不正確的..好吧,將看到..謝謝。 – NonCoder 2015-04-04 17:29:12

0

可能,這將幫助。

工作演示:HERE

注:檢查空/無選擇(可能會引發錯誤) 檢查的JS功能

HTML

<div id="main1"> 
    <span class="username">Username_1</span> 
    white some text content inside div... 
    blue some text content inside div... 
    yellow some text content inside div... 
</div> 

<div id="main2"> 
    <span class="username">Username_2</span> 
    another test1 text content inside div... 
    another test2 text content inside div... 
    another text test3 content inside div... 
</div> 

<button onclick="getSelectedSpan()">Get Span innerHTML</button> 

JS

function getSelectedSpan() { 

    // get the base node of the selected text 
    var baseNode = window.getSelection().baseNode; 

    // get the nearest parent div of base node 
    var nearestParentDiv = baseNode.parentNode.closest("div"); 

    // get the spans inside the div 
    var spanList = nearestParentDiv.getElementsByTagName("span"); 

    // first span is what you want 
    console.log(spanList[0].innerHTML); 
} 
相關問題