2013-02-07 34 views
1

如何提取與光標位置關聯的標籤。 在下面的HTML示例中,當我的光標處於「關聯」狀態時,我想獲取有關將所有標記添加到「關聯」文本的信息。如何提取與光標位置關聯的標籤

<html> 
<body> 
How <b>to <font color="#FF000">extract<i> the tags associated with </i>Cursor </b>location</font> 
</body> 
</html> 

在這裏,我想 「B,字體,我」

是否有可能得到這個信息。

+0

你的意思是常規光標,而不是插入標記爲可編輯文檔中?你什麼時候需要這些信息(即你打算處理哪個事件)? –

回答

0

我不太確定你在問什麼,但我假設你在談論正則遊標而不是插入符號。

你可以做這樣的事情:

var lastElementEntered = null; 

document.onmouseover = function(e) { 
    e = e || window.event; 
    lastElementEntered = e.target || e.srcElement; 
}; 

document.onmouseout = function() { 
    lastElementEntered = null; 
} 

function getCursorElementPath() { 
    var tagNames = []; 
    if (lastElementEntered) { 
     var node = lastElementEntered; 
     while (node && node != document.body) { 
      tagNames.unshift(node.nodeName); 
      node = node.parentNode; 
     } 
    } 
    return tagNames; 
} 

alert(getCursorElementPath());