简体   繁体   English

jQuery添加多个div容器

[英]jquery adding multiple div containers

I have a form for posting content onto a wall. 我有一张将内容张贴到墙上的表格。 We can post pictures on there and text. 我们可以在上面张贴图片和文字。 I have the form content show up in a contenteditable div so the post box displays the image people want to upload and they can type in the div also. 我在contenteditable div中显示了表单内容,因此该邮箱显示了人们想要上传的图像,他们也可以在div中键入。 I noticed though when people upload an image. 我注意到有人上传图片时。 It gets prepended like it is supposed to into the div and when they start typing it adds the container for photo around the text also. 它会像应该放入div一样被添加,并且当他们开始键入时,它也会在文本周围添加用于照片的容器。 How can I stop this from happening? 我该如何阻止这种情况的发生?

Here is my form code 这是我的表格代码

<form id="post-form" action="Scripts/create-post.php" method="post" onsubmit="return setEditable();">
    <input type="hidden" id="posted" name="posted" value=""/>
    <div id="post-message" name="post-message" contentEditable="true"></div>
    <input id="upload-pic" name="upload-pic" type="file"/>
    <button class="post-buttons" onclick="chooseFile(); return false;">Add Photo</button>
    <button class="post-buttons">Add Link</button>
    <input type="submit" value="Post" />
    <div class="cleared"></div>
</form>

Here is the jquery that adds the uploaded photo into the contenteditable div 这是将上传的照片添加到contenteditable div中的jquery

function chooseFile() {
    $("#upload-pic").click();
}

$(document).on('change', "#upload-pic", function(){
    readURL(this);
});

function readURL(input){
    if(input.files && input.files[0]){
        var reader = new FileReader();
        reader.onload = function(e){
        $('#post-message').prepend("<div id='post-image-container'><img id='preview'/></div>");
            $('#preview').attr('src', e.target.result); 
        }

        reader.readAsDataURL(input.files[0]);
    }
}

as you can see when the file is uploaded it triggers a click to add the image into the div and the img is surrounded in a div id post image container. 如您所见,当文件上传时,它会触发单击以将图像添加到div中,并且img被包围在div id post图像容器中。 This is what is being added to the text once the img is added to the div. 一旦将img添加到div,这就是添加到文本的内容。

I know this because of an alert it shows me the value of the div as 我知道这是因为有警报,它向我显示了div的值,

<div id="post-image-container"><img src='whatever the path is'></div><div id="post-image-container">hello world(if this is what the user typed in)</div>

So I managed to find out if the text is in the contenteditable div already or before it, it does not add the extra div tags around the text. 因此,我设法找出该文本是否已经在contenteditable div中或在其之前,它没有在文本周围添加额外的div标签。 It is only after the image is inserted and you hit enter to type again I think it believes there is a change to the image on the enter hit and then ever consecutive key stroke after that. 只有在插入图像之后,您再按Enter键才能再次键入,我认为它认为Enter命中图像有变化,然后在此之后进行连续击键。

Check if #post-image-container already exists and simply update it. 检查#post-image-container已存在,然后简单地对其进行更新。 If it doesn't then prepend it. 如果没有,则将其放在前面。

 function readURL(input){
        if(input.files && input.files[0]){
            var reader = new FileReader();
            reader.onload = function(e){


                if($('#post-image-container').length){

                    $('#preview').attr('src', e.target.result); 

                }else{

                    $('#post-message').prepend("<div id='post-image-container'><img id='preview'/></div>");
                        $('#preview').attr('src', e.target.result); 
                    }

                 }

            reader.readAsDataURL(input.files[0]);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM