函子蛋糕 2022-06-04 23:34 采纳率: 80.6%
浏览 127
已结题

如何在html中引入html?

html1是表单,html2是背景。这个背景可以为html格式,也可以为js格式。但都引入失败了。


```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>input练习</title>
    <link rel="import" href="background.html">
    <script src="js/mouse.js"></script>
    <style type="text/css">
        input{
            border-radius:20px;
            border:solid 1px gray;
            background:rgba(255,255,255,0.15)
        }
        .button{
            border-radius:8px;
            width:30px;
            height:30px;
            font-size:16px;
        }
        hr{/*透明渐变水平线*/
            height:1px;
            border:none;
            border-top:2px dashed #eff5fa;
            width:80%;
        }
        textarea{
            background:rgba(255,255,255,0.15);
            border:solid 1px gray;
            border-radius:15px;
        }
    </style>
</head>

<body>
    <div class="box">
        <form>
            <h3>个人信息</h3>
            <br><hr/>
            姓名:<input type="text" maxlength="10"/>
            <br>
            密码:<input type="password" name="" />
            <br>
            年龄:<input type="number" name="" />
            <br>
            性别:<input type="radio" name="gender" value="男"/><input type="radio" name="gender" value="女"/><br><hr/>
            <p>喜爱清单</p>
            喜爱的颜色:<input type="text"/><br>
            喜爱的书籍:<input type="text"/><br>
            喜爱的画作:<input type="text"/><br>
            喜爱的乐器:<input type="text"/><br>
            喜爱的歌曲:<input type="text"/><br>
            喜爱的影视:<input type="text"/><br>
            喜爱的网站:<input type="text"/><br>
            喜爱的水果:<input type="text"/><br>
            <br><hr/>
            <input type="button" style="background-color:#d0ebfc;border:solid 1px gray;" value="按钮"/>
            <input type="submit" style="background-color:#effacf;border:solid 1px gray;" value="提交"/>
            <input type="reset" style="background-color:#fcdef7;border:solid 1px gray;" value="重置" />
            <br><hr/>
            <textarea rows="5" cols="40"></textarea>
        </form>
    </div>
</body>
</html>
<!d>
<html lang="en">
    <meta charset="UTF-8">
    <title>蛛网动态背景</title>
<head>

</head>
<body>
    <script>
        window.onload=function(){
            //定义body的margin由默认值8px->0px
            document.body.style.margin="0";
            document.body.style.background="#30333F";
            //创建canvas画布
            document.body.appendChild(document.createElement('canvas'));
            var canvas = document.querySelector('canvas'),
                    ctx = canvas.getContext('2d') //ctx返回一个在canvas上画图的api/dom
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
            canvas.style.position='fixed';
            ctx.lineWidth = .3;
            ctx.strokeStyle = (new Color(150)).style;
            //定义鼠标覆盖范围
            var mousePosition = {
                x: 30 * canvas.width / 100,
                y: 30 * canvas.height / 100
            };
            var dots = {
                nb: 1000,//Dot的总数
                distance: 50,
                d_radius: 100,
                array: []
            };
            //创建颜色类,Color类返回字符串型rgba(*,*,*,.8)
            function mixComponents(comp1, weight1, comp2, weight2) {
                return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
            }
            function averageColorStyles(dot1, dot2) {
                var color1 = dot1.color,
                        color2 = dot2.color;

                var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius),
                        g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius),
                        b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
                return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
            }
            function colorValue(min) {
                return Math.floor(Math.random() * 255 + min);
            }
            function createColorStyle(r,g,b) {
                return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
            }
            function Color(min) {
                min = min || 0;
                this.r = colorValue(min);
                this.g = colorValue(min);
                this.b = colorValue(min);
                this.style = createColorStyle(this.r, this.g, this.b);
            }
            //创建Dot类以及一系列方法
            function Dot(){
                this.x = Math.random() * canvas.width;
                this.y = Math.random() * canvas.height;

                this.vx = -.5 + Math.random();
                this.vy = -.5 + Math.random();

                this.radius = Math.random() * 2;

                this.color = new Color();
            }

            Dot.prototype = {
                draw: function(){
                    ctx.beginPath();
                    ctx.fillStyle = this.color.style;
                    ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
                    ctx.fill();
                }
            };
            function moveDots() {//Dot对象的移动
                for(i = 0; i < dots.nb; i++){

                    var dot = dots.array[i];

                    if(dot.y < 0 || dot.y > canvas.height){
                        dot.vx = dot.vx;
                        dot.vy = - dot.vy;
                    }
                    else if(dot.x < 0 || dot.x > canvas.width){
                        dot.vx = - dot.vx;
                        dot.vy = dot.vy;
                    }
                    dot.x += dot.vx;
                    dot.y += dot.vy;
                }
            }
            function connectDots(){//DOt对象的连接
                for(i = 0; i < dots.nb; i++){
                    for(j = i; j < dots.nb; j++){
                        i_dot = dots.array[i];
                        j_dot = dots.array[j];

                        if((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > - dots.distance && (i_dot.y - j_dot.y) > - dots.distance){
                            if((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > - dots.d_radius && (i_dot.y - mousePosition.y) > - dots.d_radius){
                                ctx.beginPath();
                                ctx.strokeStyle = averageColorStyles(i_dot, j_dot);
                                ctx.moveTo(i_dot.x, i_dot.y);
                                ctx.lineTo(j_dot.x, j_dot.y);
                                ctx.stroke();//绘制定义的路线
                                ctx.closePath();//创建从当前点回到起始点的路径
                            }
                        }
                    }
                }
            }
            function createDots(){//创建nb个Dot对象
                for(i = 0; i < dots.nb; i++){
                    dots.array.push(new Dot());
                }
            }
            function drawDots() {//引用Dot原型链,使用draw方法,在canvas上画出Dot对象
                for(i = 0; i < dots.nb; i++){
                    var dot = dots.array[i];
                    dot.draw();
                }
            }
            function animateDots() {
                ctx.clearRect(0, 0, canvas.width, canvas.height);//清除画布,否则线条会连在一起
                moveDots();
                connectDots();
                drawDots();
                requestAnimationFrame(animateDots);
            }
            createDots();//使用创建Dot类函数
            requestAnimationFrame(animateDots);//使用canvas独有的60Hz刷新屏幕画布的方法

            document.querySelector('canvas').addEventListener('mousemove',function(e){
                mousePosition.x = e.pageX;
                mousePosition.y = e.pageY;
            })

            document.querySelector('canvas').addEventListener('mouseleave',function(e){//鼠标离开时,连接自动返回到画布中心
                mousePosition.x = canvas.width / 2;
                mousePosition.y = canvas.height / 2;
            })
        }
    </script>

</body>
</html>

```

  • 写回答

3条回答 默认 最新

  • Heerey525 前端领域新星创作者 2022-06-04 23:54
    关注

    使用js引入,js文件中直接 window.onload = function () {}

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月24日
  • 已采纳回答 10月16日
  • 修改了问题 6月4日
  • 修改了问题 6月4日
  • 展开全部

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图