问题
I've the below code with Deno runtime, which is displating a button at the website:
- How can I change the attributes of this button
- 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