如何在iframe中找到并选择“String”,然后用另一个字符串替换?,
以下代码运作正常,但我无法将它们组合在一起,
function Find(txt) {
var iframe = document.getElementById("Text_ifr");
var iframe_contents = iframe.contentDocument.body.innerHTML;
if (iframe_contents.indexOf(txt) >= 0) {
alert('Found')
}
else { alert('Not Found') }
return false;
}
function replaceSelectedText(replacementText) {
var iframe = document.getElementById("Text_ifr");
var win = iframe.contentWindow;
var doc = iframe.contentDocument || win.document;
var sel, range;
if (win.getSelection) {
sel = win.getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(doc.createTextNode(replacementText));
}
} else if (doc.selection && doc.selection.createRange) {
range = doc.selection.createRange();
range.text = replacementText;
}
}
由于
<iframe id="Text_ifr" frameborder="0" allowtransparency="true" title="Rich Text Area. Press ALT-F9 for menu. Press ALT-F10 for toolbar. Press ALT-0 for help" src='javascript:""' style="width: 100%; height: 152px; display: block;"></iframe>
<!DOCTYPE html><html style="overflow-y: hidden;"><head><style id="mceDefaultStyles" type="text/css">.mce-content-body div.mce-resizehandle {position: absolute;border: 1px solid black;background: #FFF;width: 7px;height: 7px;z-index: 10000}.mce-content-body .mce-resizehandle:hover {background: #000}.mce-content-body *[data-mce-selected] {outline: 1px solid black;resize: none}.mce-content-body .mce-clonedresizable {position: absolute;outline: 1px dashed black;opacity: .5;filter: alpha(opacity=50);z-index: 10000}.mce-content-body .mce-resize-helper {background: #555;background: rgba(0,0,0,0.75);border-radius: 3px;border: 1px;color: white;display: none;font-family: sans-serif;font-size: 12px;white-space: nowrap;line-height: 14px;margin: 5px 10px;padding: 5px;position: absolute;z-index: 10001}
.mce-visual-caret {position: absolute;background-color: black;background-color: currentcolor;}.mce-visual-caret-hidden {display: none;}*[data-mce-caret] {position: absolute;left: -1000px;right: auto;top: 0;margin: 0;padding: 0;}
.mce-content-body .mce-offscreen-selection {position: absolute;left: -9999999999px;width: 100pxheight: 100px}.mce-content-body *[contentEditable=false] {cursor: default;}.mce-content-body *[contentEditable=true] {cursor: text;}
</style><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><link type="text/css" rel="stylesheet" href="http://localhost:1344/PlugIns/tinymce/skins/lightgray/content.min.css"></head><body id="tinymce" class="mce-content-body " data-id="Text" contenteditable="true" spellcheck="false" style="overflow-y: hidden; padding-left: 1px; padding-right: 1px; padding-bottom: 50px;" data-mce-style="overflow-y: auto; padding-left: 1px; padding-right: 1px; padding-bottom: 50px;"><p> textarea is here</p></body></html>