How can I add eventListner and attribute to HTML element in Deno

孤街醉人 提交于 2020-01-05 04:11:07

问题


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

  1. How can I change the attributes of this button
  2. How can I add eventListner for this button
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()

回答1:


By replacing

<button>click me</button>\n

with

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

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`


来源:https://stackoverflow.com/questions/59558678/how-can-i-add-eventlistner-and-attribute-to-html-element-in-deno

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!