2
時,使用我碰到下面的怪癖(在FF3測試)來了jQuery的對話框保存文本選擇:如何打開一個jQuery對話框
- 用戶選擇文本
- 在代碼中,開闢一個jQuery對話框
- BUG:文本獲取未選擇
(文本可以在一個文本或只是網頁上的HTML)
因此,對我來說,這似乎是一個有趣(令人討厭)的錯誤或怪癖,但也許有一個很好的解釋。 最讓我感興趣的是如何在打開對話框後保留此文字選擇?
下面是一些代碼:
function getSelectedText() {
var t;
if (d.getSelection) t = d.getSelection();
else if(d.selection) t = d.selection.createRange();
if (t.text != undefined) t = t.text;
if (!t || t=='') {
var a = d.getElementsByTagName('textarea');
for (var i = 0; i < a.length; ++i) {
if (a[i].selectionStart != undefined && a[i].selectionStart != a[i].selectionEnd) {
t = a[i].value.substring(a[i].selectionStart, a[i].selectionEnd);
break;
}
}
}
return t;
}
$("#dialog").dialog({
autoOpen: false,
bgiframe: false,
height: 60,
width: 80,
modal: false,
show: 'highlight',
title: 'wc'});
alert(getSelectedText()); // Text is here
$("#dialog").dialog("open");
alert(getSelectedText()); // Text is not selected here :(damn!
謝謝!
據我所知,這是正常的行爲。如果您選擇文字,然後點擊頁面上的其他任何位置,選擇就會消失。 – Geoff 2009-05-05 13:34:08