Javascript 如何使用我自己的所见即所得编辑器插入图像

Javascript 如何使用我自己的所见即所得编辑器插入图像,javascript,image,insert,editor,wysiwyg,Javascript,Image,Insert,Editor,Wysiwyg,我正在使用iframe构建自己的所见即所得编辑器,我想找到一种简单插入图像的方法我已经使用JS获得了粗体样式、斜体样式、H1和H2: function bold(){ editor.document.execCommand("bold", false, null) } function italic(){ editor.document.execCommand("italic", false, null) } function h1()

我正在使用iframe构建自己的所见即所得编辑器,我想找到一种简单插入图像的方法我已经使用JS获得了粗体样式、斜体样式、H1和H2:

    function bold(){
    editor.document.execCommand("bold", false, null)
    }

    function italic(){
    editor.document.execCommand("italic", false, null)

    }
    function h1(){
    editor.document.execCommand('formatBlock',false,'h1');
    }

    function h2(){
    editor.document.execCommand('formatBlock',false,'h2');
    }
这很好,但是我正在努力使用“插入图像按钮” 因为我对编码非常陌生,所以我尝试了以下方法:

function image(){
var image = prompt ("Paste or type a link", "http://")
editor.document.execCommand("createImage", false, image)
但这不起作用。我的意图是,用户可以从他们的计算机和/或互联网上传图像。请,如果有人知道如何插入图像。。。救命啊


非常感谢

您是否尝试过使用相对链接格式化图像位置,其中@符号位于图像位置之前。你的代码和解释也没有解释他们是从他们的机器上传图像到像我的文档这样的位置,还是严格地从和http链接上传图像

要使用Javascript和HTML从本地计算机上载,您可以签出此代码。您的问题是在编写自定义Javascript函数时经常出现的问题

<script type='text/javascript'>

function main()
{
var inputFileToLoad = document.createElement("input");
inputFileToLoad.type = "file";
inputFileToLoad.id = "inputFileToLoad";
document.body.appendChild(inputFileToLoad);

var buttonLoadFile = document.createElement("button");
buttonLoadFile.onclick = loadImageFileAsURL;
buttonLoadFile.textContent = "Load Selected File";
document.body.appendChild(buttonLoadFile);
}

function loadImageFileAsURL()
{
var filesSelected = document.getElementById("inputFileToLoad").files;
if (filesSelected.length > 0)
{
    var fileToLoad = filesSelected[0];

    if (fileToLoad.type.match("image.*"))
    {
        var fileReader = new FileReader();
        fileReader.onload = function(fileLoadedEvent) 
        {
            var imageLoaded = document.createElement("img");
            imageLoaded.src = fileLoadedEvent.target.result;
            document.body.appendChild(imageLoaded);
        };
        fileReader.readAsDataURL(fileToLoad);
    }
}
}

main();

</script>

函数main()
{
var inputFileToLoad=document.createElement(“输入”);
inputFileToLoad.type=“文件”;
inputFileToLoad.id=“inputFileToLoad”;
document.body.appendChild(inputFileToLoad);
var buttonLoadFile=document.createElement(“按钮”);
buttonLoadFile.onclick=loadImageFileAsURL;
buttonLoadFile.textContent=“加载所选文件”;
document.body.appendChild(buttonLoadFile);
}
函数loadImageFileAsURL()
{
var fileselected=document.getElementById(“inputFileToLoad”).files;
如果(fileselected.length>0)
{
var fileToLoad=fileselected[0];
if(fileToLoad.type.match(“image.*))
{
var fileReader=newfilereader();
fileReader.onload=函数(fileLoadedEvent)
{
var imageLoaded=document.createElement(“img”);
imageLoaded.src=fileLoadedEvent.target.result;
document.body.appendChild(imageLoaded);
};
readAsDataURL(fileToLoad);
}
}
}
main();
您可以在此处查看有关使用Javascript和HTML5上传图像的完整文章:

它应该是“insertimage”,而不是createImage

function image(){var image = prompt ("Paste or type a link", "http://")editor.document.execCommand("insertimage", false, image)}

约翰尼,谢谢你回答我的问题。嗯,我想上传一张图片,无论是从我的电脑还是从互联网。最好是从我的电脑,但两者都可以帮助。:)哇!我只懂那50%的代码,但我试过了,效果很好!!现在我必须做一些修改,现在就足够了!谢谢你,约翰尼!编写自己的WYISWYG编辑器是最糟糕的想法之一。最好使用已经开发多年的现有功能之一,并且有一个团队支持修复不同浏览器中围绕此功能存在的无数错误