javascript 单击后不显示 DIV onclick 中的 alert()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10649264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:33:14  来源:igfitidea点击:

alert() in DIV's onclick is not displayed after click

javascriptcsshtml

提问by mehmood

I have two rectangles with some text inside. I would like to display a different alert when I click on each one, but code below doesn't work (alert is not displayed). Any ideas? Thanks

我有两个矩形,里面有一些文字。我想在单击每个警报时显示不同的警报,但下面的代码不起作用(未显示警报)。有任何想法吗?谢谢

<div id="panel0" style="width: 100%">
    <div class="titleBox" id="rect0" onclick="alert('yeah1');"
        style="height: 85px; width: 85px;">Hello</div>
    <div class="titleBox" id="rect1" onclick="alert('yeah2');"
        style="height: 85px; width: 85px;">Hello</div>
</div>

回答by mehmood

Try this

试试这个

<div id="rect0" onclick="alert('yeah1');" style="height: 85px; width: 85px; background-color: #ff0000;">
     Hello
</div>