This article describes the method of inserting content in the cursor position of the JS editor in the iframe editor. Share it for your reference, as follows:
<html><iframe id="x" name="x"></iframe><input type="button" onclick="t()" value="test"><input type="button" onclick="frames['x'].location.href='about:blank';" value="clear"><script>//setTimeout('window.frames["x"].document.designMode="On"',200);function t(){window.frames["x"].document.designMode="On";var html = '<b style="color:red">'+$('xx').value+'</b>';//The inserted content (html) can be a picture. if(getBrowser()=='ie'){var Editor = window.frames["x"];//IE gets the iframe method, otherwise the image position will run to the top of the page. Editor.focus();o=Editor.document.selection.createRange();o.pasteHTML(html);}else if(getBrowser()=='chrome'){var Editor = $('x');//firefox must obtain nodes in this way Editor.focus();//alert(Editor.contentWindow.getSelection().getRangeAt(0));var rng = Editor.contentWindow.getSelection().getRangeAt(0);var frg = rng.createContextualFragment(html);rng.insertNode(frg);}}//Get browser version function getBrowser(){var agentValue = window.navigator.userAgent.toLowerCase();if(agentValue.indexOf('msie')>0){return "ie";}else if(agentValue.indexOf('firefox')>0){return "ff";}else if(agentValue.indexOf('chrome')>0){return "chrome";}}function $(id){return document.getElementById(id);}//Get element according to the element className attribute. If multiple elements have the same style class name, you can use index to specify which element to return. The first one is 1function getNodeByClassName(vclassname,index){//var allnodes = document.all;var allnodes = document.getElementsByTagName("*");var x = 0;for(var i=0;i<allnodes.length;i++){if(allnodes[i].className==vclassname){if(index!="undefined"){x++;if(x<index){continue;}} return allnodes[i];}}}</script>Input: <input id="xx">For more information about JavaScript related content, please check out the topics of this site: "Summary of JSON operation techniques in JavaScript", "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm techniques", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm techniques", "Summary of JavaScript traversal algorithms and techniques" and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.