javascript - 单击按钮 Javascript 后仅显示图像

标签 javascript jquery html image

我这里有这段代码,每天都会显示一个新图像。它有效,但单击按钮后仅显示图像。页面上的其余文本消失。有没有办法可以在页面上同时显示照片和文本?

<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Want to see a picture?</h1>

<h2>Picture Display</h2>
</p><p>Some Text Here Some Text Here Some Text Here Some Text Here</p>
<button onclick="loadList()">Click Here!</button>
<script type="text/javascript"><!--
    var imlocation = "images/";
    function ImageArray (n) {
        this.length = n;
        for (var i =1; i <= n; i++) {
            this[i] = ' '
        }
    }
    image = new ImageArray(90);
    image[0] = 'CPR1.jpg';
    image[1] = 'CPR2.jpg';
    image[2] = 'CPR3.jpg';

    var currentdate = new Date();
    var imagenumber = currentdate.getDay();

    function loadList() {
        document.write('<img src="' + imlocation + image[imagenumber] + 
'">');
        style.display = "none";
    }

    //--></script>
</body>
</html>

最佳答案

document.write主要用于测试:如果在 HTML 文档完全加载后使用,它将删除所有现有的 HTML

您可以使用innerHTML附加图像标签

var imlocation = "images/";

function ImageArray(n) {
  this.length = n;
  for (var i = 1; i <= n; i++) {
    this[i] = ' '
  }
}
image = new ImageArray(90);
image[0] = 'CPR1.jpg';
image[1] = 'CPR2.jpg';
image[2] = 'CPR3.jpg';

var currentdate = new Date();
var imagenumber = currentdate.getDay();

function loadList() {
  document.getElementById('insertPicture').innerHTML=('<img src="' + imlocation + image[imagenumber] +
    '">');

}
<h1>Want to see a picture?</h1>

<h2 id='insertPicture'>Picture Display</h2>

<p>Some Text Here Some Text Here Some Text Here Some Text Here</p>
<button onclick="loadList()">Click Here!</button>

关于javascript - 单击按钮 Javascript 后仅显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43896973/

相关文章:

javascript - React-Router V4 - 将 Props 传递给 Route Component 中的 Render 函数以生成新组件

jQuery 无限循环遍历每个 div

jquery - jQuery.ajax 中是否弃用 success 参数?

jquery - 删除第一列后保留表格高度 'tr'

html - CSS 下拉菜单仅在左侧扩展

javascript - 附加图像 ID

html - CSS - 阻止 :hover during animation?

javascript - 从状态返回数据对象 - 无法读取未定义的属性 '0'

javascript - 为嵌套的 rest url 定义 ember 数据模型

javascript - 使用javascript计算textarea中的字节数