分时显示不同图片和问候语

该博客展示了一个根据系统时间动态改变图片和问候语的HTML页面。通过JavaScript获取当前小时数,使用分支语句设置了不同时间段对应的图片和问候语,如上午、中午、下午和晚上的问候,并相应地修改了img元素的src属性和div元素的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        //根据系统不同时间判断,需要用到日期内置对象

        //利用分支语句设置不同图片

        //修改img元素的src属性

        //需要一个div显示问候语,跟随时间修改元素内容

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img {
            width: 200px;
            height: 160px;
            margin-bottom: 10px;
        }
    </style>
</head>

<body>
    <img src="./image/上午.jpg" alt="#">
    <div>上午好</div>
    <script>
        var div = document.querySelector('div');
        var img = document.querySelector('img');
        var date = new Date();
        var h = date.getHours();
        if (h < 10) {
            img.src = './image/上午.jpg';
            div.innerHTML = '上午好~';
        } else if (h < 13) {
            img.src = './image/中午好.png';
            div.innerHTML = '中午好~';
        } else if (h < 18) {
            img.src = './image/下午好.png';
            div.innerHTML = '下午好~';
        } else if (h < 24) {
            img.src = './image/晚上好.gif';
            div.innerHTML = '晚上好~';
        }

       // alert(h);//检查是否获取到时间
    </script>
</body>

</html>

gradio入门苏黎世的从前927于 2024-12-31 22:08:37 发布阅读量955 收藏 23点赞数 27分类专栏: 工具 文章标签: 前端框架版权工具专栏收录该内容1 篇文章订阅专栏快速入门import gradio as gr def greet(name): return "h1 " + name + "!" gr.Interface( fn=greet, inputs="text", outputs="text", title="Greeting Interface", description="This interface greets the user with the provided name." ).launch(share=True)文件名为app.py,直接在终端运行python app.py。即会出现一个链接,打开链接,就会在浏览器界面出现内容。但是使用这种方法,当需要更改代码时,更改后,需要将服务停掉,重新运行,很不方便。所以,第二种方法,使用debug模式运行。方法:将接口赋值给demo(固定写法,debug模式必须在demo的命名空间下启动),然后用demo启动,最后在终端运行gradio app.pyimport gradio as gr def greet(name): return "h1 " + name + "!" demo = gr.Interface( fn=greet, inputs="text", outputs="text", title="Greeting Interface", description="This interface greets the user with the provided name." ) demo.launch(share=True)Gradio 学习笔记:构建简单的 AI 交互界面1. Gradio 简介Gradio 是一个 Python 库,可以快速为机器学习模型创建友好的 Web 界面。它的特点是:简单易用,几行代码即可创建界面支持多种输入输出类型可以快速分享部署适合原型开发演示2. 基本安装使用# 安装pip install gradio# 基本导入import gradio as gr3. 常用组件3.1 输入组件gr.Textbox(): 文本输入框gr.Number(): 数字输入框gr.Slider(): 滑动条gr.Radio(): 单选按钮gr.Checkbox(): 单个复选框gr.CheckboxGroup(): 多选框组gr.Image(): 图片上传gr.Audio(): 音频上传3.2 输出组件gr.Textbox(): 文本显示gr.Label(): 标签显示gr.Image(): 图片显示gr.Plot(): 图表显示4. 界面布局4.1 基本布局元素with gr.Blocks() as demo:    # Markdown 支持    gr.Markdown("# 标题")        # 标签页    with gr.Tab("标签1"):        # 内容        # 行布局    with gr.Row():        # 并排组件5. 实战示例:多功能演示程序import gradio as grdef greet(name, is_shouting=False):    if is_shouting:        return f"你好, {name.upper()}!"    return f"你好, {name}!"def calculator(num1, num2, operation):    if operation == "加":        return str(num1 + num2)    elif operation == "减":        return str(num1 - num2)    elif operation == "乘":        return str(num1 * num2)    elif operation == "除":        return str(num1 / num2) if num2 != 0 else "除数不能为零"with gr.Blocks() as demo:    gr.Markdown("# 多功能演示程序")        with gr.Tab("问候程序"):        gr.Markdown("## 问候功能")        with gr.Row():            name = gr.Textbox(label="请输入您的名字")            is_shouting = gr.Checkbox(label="大写模式")            greet_output = gr.Textbox(label="问候语")        name.change(fn=greet, inputs=[name, is_shouting], outputs=greet_output)        is_shouting.change(fn=greet, inputs=[name, is_shouting], outputs=greet_output)        with gr.Tab("计算器"):        gr.Markdown("## 计算器功能")        with gr.Row():            num1 = gr.Number(label="第一个数")            num2 = gr.Number(label="第二个数")            operation = gr.Radio(["加", "减", "乘", "除"], label="运算")            result = gr.Textbox(label="结果")                calculate_btn = gr.Button("计算")        calculate_btn.click(            fn=calculator,            inputs=[num1, num2, operation],            outputs=result        )demo.launch()6. 事件处理Gradio 支持多种事件:click(): 点击事件change(): 值改变事件submit(): 提交事件7. 部署分享本地运行:demo.launch()公开分享:demo.launch(share=True)自定义端口:demo.launch(server_port=7860)8. 实用技巧1. 使用 gr.Markdown() 添加格式化文本使用 with gr.Row() 创建水平布局使用 with gr.Tab() 创建多标签页界面使用 value= 参数设置默认值使用 label= 参数设置组件标签9. 注意事项1. 确保函数输入输出与界面组件匹配2. 处理异常情况(如除零错误)合理组织界面布局,提高用户体验添加适当的提示信息说明文字10. 总结Gradio 是一个强大而简单的工具,特别适合:快速创建演示界面展示机器学习模型创建简单的 Web 应用原型验证测试通过这些基础知识,你已经可以创建实用的交互界面了。随着深入学习,你还可以探索更多高级功能,如自定义主题、API集成等。
最新发布
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值