как получить выделенный текст от iframe с JavaScript?

, Если Вы используете.NET в проводнике решений, щелкают правой кнопкой по Вашей программе и выбирают свойства. Под ресурсом раздел выбирают Значок и декларацию, затем просматривают к местоположению Вашего значка.

17
задан akjoshi 28 January 2013 в 07:33
поделиться

3 ответа

document.getSelection

Is on the outer document. To get the selection of the document in the iframe you need to grab the inner document:

var iframe= document.getElementById('my');
var idoc= iframe.contentDocument || iframe.contentWindow.document; // ie compatibility

idoc.getSelection()

Note however that WebKit does not support document.getSelection() or document.selection. Try replacing it with window.getSelection() which works in both Firefox and WebKit, but returns a selection object (a collection/wrapper around Ranges), which needs stringing:

var idoc= iframe.contentDocument || iframe.contentWindow.document;
var iwin= iframe.contentWindow || iframe.contentDocument.defaultView;

''+iwin.getSelection()

I'm not sure what the point of this is:

if (window.RegExp) {
  var regstr = unescape("%20%20%20%20%20");
  var regexp = new RegExp(regstr, "g");
  str = str.replace(regexp, "");
}

RegExp is basic JavaScript dating back to the very earliest version; it will always be there, you don't have to sniff for it. The URL-encoding of multiple spaces is quite unnecessary. You don't even need RegExp as such, a string replace could be written as:

str= str.split('     ').join('');
17
ответ дан 30 November 2019 в 12:20
поделиться

Вы не можете получить доступ к данным внутри iframe , которые принадлежат другому домену. Это связано с Политикой одинакового происхождения .

6
ответ дан 30 November 2019 в 12:20
поделиться

You need to get the selection from the document/window in the iframe.

function getIframeSelectionText(iframe) {
  var win = iframe.contentWindow;
  var doc = win.document;

  if (win.getSelection) {
    return win.getSelection().toString();
  } else if (doc.selection && doc.selection.createRange) {
    return doc.selection.createRange().text;
  }
}

var iframe = document.getElementById("my");
alert(getIframeSelectionText(iframe));
6
ответ дан 30 November 2019 в 12:20
поделиться
Другие вопросы по тегам:

Похожие вопросы: