2011-05-31 20 views
16

में एक चयन मैं अब के बाद मैं मैं चयन मैं इसे साफ करने की आवश्यकता के साथ क्या करने की जरूरत करने के लिए इस समारोहसाफ़ फ़ायरफ़ॉक्स

function smth() { 
var container = null; 
var newContainer = null; 
if (window.getSelection) { // all browsers, except IE before version 9 
    alert("first if"); 
    var selectionRange = window.getSelection(); 
    if (selectionRange.rangeCount > 0) { 
     var range = selectionRange.getRangeAt(0); 
     container = range.commonAncestorContainer; 
     newContainer = container; 
    } 
} 
else { 
    if (document.selection) { // Internet Explorer 
     alert("second if"); 
     var textRange = document.selection.createRange(); 
     container = textRange.parentElement(); 
    } 
} 

if (newContainer) { 
    return newContainer.nodeName; 
} 
else { 
    alert("Container object for the selection is not available!"); 
} 
}  

है। मैंने कुछ चीजों की कोशिश की, कुछ भी काम नहीं किया?

document.selection.clear()  

यह काम नहीं किया।

उत्तर

58

समस्याग्रस्त ब्राउज़र के लिए:

document.selection.empty() 

अन्य ब्राउज़र के लिए:

window.getSelection().removeAllRanges() 

देखें http://help.dottoro.com/ljigixkc.php

+0

धन्यवाद, यह – Ovi

+0

(वास्तव में एक iframe) एक TinyMCE संपादक विंडो के अंदर उदाहरण के लिए के रूप में की उम्मीद काम नहीं करेंगे मदद की। आपको इसका उपयोग करने की आवश्यकता होगी: tinymce.activeEditor.selection.collapse(); – Recoil

+1

"समस्याग्रस्त ब्राउज़र के लिए" अब मामला नहीं है। बाद का विकल्प आईई के हाल के संस्करणों सहित सभी ब्राउज़रों के लिए काम करता है। (मान लीजिए कि आप "समस्याग्रस्त" द्वारा आईई का मतलब है) – Porschiey

0

नोट: मामले में आप तो अपने एक इनपुट या पाठ क्षेत्र तत्व का टेक्स्ट चयन कर रहे हैं यदि आप मानक देशी HTML तत्व इनपुट या textarea का चयन विधि का उपयोग करेंगे, तो कोड में अधिक क्रॉस ब्राउज़र समर्थन होगा।

यदि कोई देशी इनपुट या टेक्स्टरेरा तत्व मूल चयन विधि का उपयोग करके चुना गया था तो उपर्युक्त विधियों का उपयोग करके मेरे फ़ायरफ़ॉक्स 44.0.2 पर काम नहीं करता है। इसके लिए क्या काम किया, और मुझे लगता है कि सभी ब्राउजर पर काम करता है, निम्न कोड चला रहा है जो एक नया तत्व बनाता है और इसे चुनता है। नया तत्व display:none या visibility:hidden के साथ नहीं हो सकता है क्योंकि तब यह मेरे फ़ायरबॉक्स में नहीं चुना जाता है, इसलिए चाल सभी आकार विशेषताओं को 0\none पर मजबूर कर अदृश्य बनाना है।

var tempElement = document.createElement("input"); 
tempElement.style.cssText = "width:0!important;padding:0!important;border:0!important;margin:0!important;outline:none!important;boxShadow:none!important;"; 
document.body.appendChild(tempElement); 
tempElement.select(); 
/* Use removeChild instead of remove because remove is less supported */ 
document.body.removeChild(tempElement); 
+0

यह काम करता है, लेकिन इनपुट फ़ील्ड से फोकस चुराता है। –

+0

@ लॉरेंसडोल फिर आप इसे चलाने के बाद इनपुट फ़ील्ड को फिर से शुरू कर सकते हैं, अगर फोकस खोना आपके लिए एक समस्या है। –

0

उपयोग tinymce.activeEditor.selection.collapse() है कि अगर काम नहीं होता है का उपयोग const range = tinymce.activeEditor.dom.createRng(); tinymce.activeEditor.selection.setRng(range)