动态改变html元素 [英] dynamically change html element

查看:22
本文介绍了动态改变html元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些复选框,一旦我选中其中一个复选框,复选框的 ID 就会发送到一个函数.我希望此函数将复选框更改为具有相同 ID 的 元素.

I have some checkboxes, once I check one of these the ID of the checkbox is sent to a function. I want this function to change the checkbox to a <img>element with the same ID.

我试过了

document.getElementById(ID).innerHTML = '<img src="image.png" id="' + ID + '"/>';

但这不起作用,是否可以更改元素,或者我是否需要使用相同的 ID 创建一个新元素?

But this doesn't work, is it possible to change a element or would I need to create a new one using the same ID?

谢谢

推荐答案

我建议与其将一个元素替换为另一个元素,不如让一个元素可见,一个不可见,例如...

I would advise that rather than replace one element with another you make one visible and one invisible eg...

document.getElementById(ID).style.display='none';

document.getElementById(ID).style.display='block';

我会想办法绕过相同的 ID 约束.为什么需要他们拥有完全相同的 ID?

And I would think of a way round the same ID constraint. Why do you need them to have exactly the same ID?

这篇关于动态改变html元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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