我才刚刚开始深入研究 javascript,对如何使用 jQuery 还没有真正的了解,但我正在开发一个带有控制面板的页面。我试图通过单击控制面板上的按钮来更改控制面板的功能。
这是我到目前为止所拥有的;
html:
<div id="functions">
<button id="prob" onclick="myproblem()" type="button" title="prob"></button>
<button id="button1" onclick="works1()" type="button" title="button1"></button>
<button id="button2" onclick="works2" type="button" title="button2"></button>
</div>
Javascript:
function myproblem() {
document.getElementById("functions").html('
<button id="prob" onclick="myproblem()" type="button" title="prob"></button>
<button id="button3" onclick="works3()" type="button" title="button3"></button>
<button id="button4" onclick="works4()" type="button" title="button4"></button>');
}
我想用来更改 html 的按钮包含在正在更改的 div 中。该 div 中还有其他 onclick 函数,它们工作正常,直到我将 myproblem() 代码写入我的 js 文件中。有了那段代码,我的 js 都不起作用。
这是一个本地主机页面,它不会在线。
我假设我需要使用 jQuery 来实现这一点,但我不知道如何实现。
谢谢
最佳答案
使用innerHTML()
JavaScript 中的方法:
function myproblem() {
document.getElementById("functions").innerHTML ='<button id="prob" onclick="myproblem()" type="button" title="prob"></button> <button id="button3" onclick="works3()" type="button" title="button3"></button> <button id="button4" onclick="works4()" type="button" title="button4"></button>'
}
在 Jquery 中,您可以使用 html
方法
$(".functions").html('Your html code goes here')
关于javascript - 如何完全改变div onclick的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50493406/