草庐IT

javascript - 向 div 添加 onclick 事件

我不知道这是否被允许甚至可能。是否可以创建一个带有onclick事件的div元素,以便如果单击div区域中的任何位置,该事件将运行。例如脚本:vardivTag=document.createElement("div");divTag.id="new-ID";varonClickCommand="printWorking()"//printworkingisanothermethodthatsimplyprint"I'mworking"totheconsoledivTag.onclick=onClickCommand;console.log("onclickcommandadded")

javascript - 向 div 添加 onclick 事件

我不知道这是否被允许甚至可能。是否可以创建一个带有onclick事件的div元素,以便如果单击div区域中的任何位置,该事件将运行。例如脚本:vardivTag=document.createElement("div");divTag.id="new-ID";varonClickCommand="printWorking()"//printworkingisanothermethodthatsimplyprint"I'mworking"totheconsoledivTag.onclick=onClickCommand;console.log("onclickcommandadded")