简体   繁体   English

如何通过JavaScript在Wordpress帖子中上传图片

[英]How to upload image in wordpress post via javascript

Objective: To Load the image after the the specific time interval of 5 Second. 目的:在5秒的特定时间间隔后加载图像。

Problem: The written code is perfectly working with Single.Php, but not working inside the WordPress Post. 问题:编写的代码可以与Single.Php完美配合,但不能在WordPress Post内部使用。

    <div id="imageContainer"></div>
<script>
var img = document.createElement("img");  

img.onload = function(e) {
    var container = document.getElementById("imageContainer");
    container.appendChild(e.target);
}

setInterval( 'img.setAttribute("src","image.jpg")',5000);
</script>

The issue is that you're passing a string to setInterval , not code. 问题是您正在将字符串传递给setInterval ,而不是代码。 Pass a function instead, as in the example here. 而是传递一个函数,如此处的示例所示。 For more on the topic see the MDN documentation . 有关该主题的更多信息,请参见MDN文档

 var img = document.createElement("img"); img.onload = function(e) { var container = document.getElementById("imageContainer"); container.appendChild(e.target); } setInterval(function() { img.setAttribute("src", "https://www.gravatar.com/avatar/c2dc29e1a1ce74f25adf39e28d23a601?s=128&d=identicon&r=PG") }, 5000); 
 <div id="imageContainer"></div> 

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

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