如何在Deno中向HTML元素添加eventListner和属性 [英] How can I add eventListner and attribute to HTML element in Deno

查看:143
本文介绍了如何在Deno中向HTML元素添加eventListner和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中使用 Deno 运行时,该运行时在网站上放置了一个按钮:

I've the below code with Deno runtime, which is displating a button at the website:

  1. 如何更改此按钮的属性
  2. 如何为该按钮添加eventListner

import { serve } from "https://deno.land/std@v0.24.0/http/server.ts"

async function main() {
    const body = new TextEncoder().encode(`<button>click me</button>\n`);
    const s = serve({ port: 8000 });
    console.log(`Server had been started at: http://localhost:8000/`);
    for await (const req of s) {
      req.respond({ body });
    }
};

main()

推荐答案

通过替换

<button>click me</button>\n

使用

<button id='button' onclick="document.getElementById('button').innerHTML='wow I changed'">click me</button>\n

或另一个示例,其中分别添加eventListener

or another example where you add the eventListener separately

`<button id='button'>click me</button><script>document.getElementById('button').addEventListener('click',function(){document.getElementById('button').innerHTML='wow I changed';});</script>\n`

这篇关于如何在Deno中向HTML元素添加eventListner和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆