2012-06-22 59 views
2

在用戶按下CTRL + Home時的可滿足元素中,我試圖讓每個瀏覽器都將插入符位置移動到第一段的開頭。JavaScript:設置插入位置爲開頭的第一段(Opera bug)

讓我們假設整個頁面是可編輯的,除了剛纔的直接目標之外,我們沒有考慮任何其他內容。

這在Firefox,Safari和IE 10中工作正常,但Opera 12拒絕服從。這裏的代碼...

var s = window.getSelection(); 

if (e.ctrlKey && e.keyCode==36) 
{ 
var p0 = document.getElementsByTagName('p')[0]; 

if (p0.firstChild.nodeName.toLowerCase()=='#text') 
{//<p>text 
    var p = p0.firstChild; 
} 
else if (p0.firstChild.firstChild.nodeName.toLowerCase()=='#text') 
{//<p><em>text 
    var p = p0.firstChild.firstChild; 
} 

if (typeof p=='object') 
{ 
    s.getRangeAt(0).setStart(p,0); 
    s.getRangeAt(0).setEnd(p,0); 
    s.collapseToStart(); 
} 
} 

不要編輯我的帖子,如果你不明白的東西評論,我會很樂意澄清。

回答

0

此問題是由Opera中段落的CSS邊距引起的。爲什麼?我完全不知道,只是這個最小的測試用例重現了它。

解決方法是主觀的,邊距和填充有不同的用途,在我的情況下,我無法利用填充來代替邊距,從而使我依靠Opera來修復這個錯誤。

example.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
<title>Opera Contenteditable margin CTRL+Home/End Bug</title> 
<style type="text/css"> 
p 
{ 
/*margin: 0px 20px 14px 24px;*/ 
margin-top: 0px; 
margin-right: 20px; 
margin-bottom: 14px; 
margin-left: 24px; 
} 
</style> 
</head> 

<body> 

<div contenteditable="true"> 
<p>Paragraph one.</p> 
<p>Paragraph two.</p> 
</div> 

</body> 
</html> 
+0

[上my.opera.com並行線程](http://my.opera.com/community/forums/topic.dml?id=1449662)。 – ricksmt

相關問題