[英]Trying to display data from a database in HTML tags using Javascript
I'm trying to call a function in my home page that will change the content of an article in HTML from placeholders to info from a database before the page loads. 我试图在我的主页中调用一个函数,该函数将在页面加载之前将HTML中的文章内容从占位符更改为数据库信息。 However I keep getting an error saying that I'm missing a formal parameter.
但是我总是收到一个错误消息,说我缺少一个正式参数。 What have I done wrong?
我做错了什么?
The HTML: HTML:
<article id="article1">
<img src="images/sadface.png" alt="The image didn't load, sorry" id="img0">
<h4 id="heading0">Not working</h4>
<p id="p0">This item hasn't loaded, sorry</p>
<script>
window.onload = function splashFunction(0);
</script>
</article>
And the Javascript: 和Javascript:
function splashFunction(x){
document.getElementById("img" + x).src="images/cart2.jpg";
document.getElementById("heading" + x).innerHTML="cart";
document.getElementById("p" + x).innerHTML="cart";
};
In order to change your DOM with a call to your database you will have to use AJAX , I hope you know that. 为了通过调用数据库来更改DOM,您必须使用AJAX ,希望您知道这一点。
As for your code, you are defining the function
again on window.onload
. 至于您的代码,您将在
window.onload
再次定义该function
。 To call the function just use its name, as such: 要调用该函数,只需使用其名称,如下所示:
window.onload = splashFunction(0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.