简体   繁体   English

如何使用JavaScript添加img元素?

[英]How can I add an img element using javascript?

In my website, the user has an a text area where he/she can enter text or images for a post then clicks the a button. 在我的网站上,用户有一个文本区域,他/她可以在其中输入帖子的文本或图像,然后单击a按钮。 The button clicked is linked to a java script function that dynamically creates an article element containing the post content and the username and profile picture. 单击的按钮链接到Java脚本函数,该函数动态创建一个文章元素,其中包含帖子内容以及用户名和个人资料图片。 The code I have creates the new article element alright, but I am facing two problems: 我拥有的代码可以创建新的article元素,但是我面临两个问题:

  1. Whenever I click post again, it creates a new article element inside the previous one. 每当我再次单击发布时,它都会在前一个元素内创建一个新的article元素。
  2. I need the img element created to not have a fixed src. 我需要创建的img元素没有固定的src。

Here is my html for this part 这是我这部分的HTML

<article class="posts">
   <img  id = "profilePic" class="peopletofollowimg" src=Profile.jpg alt="View Profile Picture">
    <textarea id="posting-area" rows="6" cols="90" placeholder="Share Your Thoughts!"></textarea>



    <button onclick="createPost()" id="post-button">Post!</button>

</article>

<article id="myarticle" class="posts">

</article>

And here is my js code 这是我的js代码

<script>

document.getElementById("post-button").onclick = createPost;
var el = document.getElementById("post-button");
if (el.addEventListener)
    el.addEventListener("click", createPost(), false);
else if (el.attachEvent)
    el.attachEvent('onclick', createPost());

function createPost(){
    var article = document.createElement("article");
    article.setAttribute("id", "myarticle");
    article.className = "posts";

    var p = document.createElement("p");
    //p.setAttribute("id", "postContent");
    p.innerHTML = document.getElementById("posting-area").value;

    var img = document.createElement("img");
    //img.setAttribute=("src", document.getElementById("profilePic"));
    // img.innerHTML = document.getElementById("profilePic");
    img.getAttribute("src");
    img.className = "peopletofollowimg";

    // test case, append the content
    document.body.appendChild(article);
    document.getElementById("myarticle").appendChild(p);
    document.getElementById("myarticle").appendChild(img);

}
 </script>

IDs must be unique. ID必须是唯一的。 You're creating new elements with the same ID as the one on the page, so when you do .getElementById("myarticle") , it always selects the first one it finds. 您正在创建具有与页面上相同的ID的新元素,因此当您执行.getElementById("myarticle") ,它总是选择找到的第一个.getElementById("myarticle")

Instead of creating and appending the element and then selecting to add its content, just add the content immediately. 无需创建和附加元素,然后选择添加其内容,而只需立即添加内容。

function createPost(){
    var article = document.createElement("article");
    article.className = "posts";

    var p = document.createElement("p");
    p.innerHTML = document.getElementById("posting-area").value;

    var img = document.createElement("img");
    img.className = "peopletofollowimg";

    article.appendChild(p);
    article.appendChild(img);

    document.body.appendChild(article);
}

Not sure what you meant about the img not having a fixed src , but you can easily give it any value you want. 不知道关于没有固定srcimg意味着什么,但是您可以轻松地为其提供所需的任何值。

img.src = "some/path/to/image.png";

Like Barmar said about binding the handlers, you shouldn't be invoking the function. 就像Barmar关于绑定处理程序所说的那样,您不应该调用该函数。 Right now you're only binding the first. 现在,您只绑定第一个。 If you fix the invocation, you'll end up binding it twice. 如果您修复了调用,最终将其绑定两次。

Do this instead: 改为这样做:

var el = document.getElementById("post-button");

if (el.addEventListener)
    el.addEventListener("click", createPost, false);
else if (el.attachEvent)
    el.attachEvent('onclick', createPost);
else
    el.onclick = createPost;

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

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