gpt4 book ai didi

javascript - 将事件监听器添加到内部类

转载 作者:行者123 更新时间:2023-11-28 07:01:16 25 4
gpt4 key购买 nike

我正在尝试将事件监听器添加到“子类”(这就是它的名字吗?)。但未能生效。

JS:

var Station_Button = function(status_id) 
{
var alert_txt, class_txt;

switch( status_id )
{
case 1:
{
alert_txt = "<strong>Complete!</strong> This Station is complete.";
class_txt = "alert alert-success";
}
case 2:
{
alert_txt = "<strong>Error!</strong> This Station has failed!";
class_txt = "alert alert-info";
}
}

document.getElementById("notify").innerHTML = alert_txt;
document.getElementById("notify").className = class_txt;
return false;
}

var ta_butt = document.getElementsByClassName("ta_butt.error");

for(var i=0;i<ta_butt.length;i++)
{
ta_butt[i].addEventListener('click', function() {Station_Button(2)}, false);
}

如果我去掉 .error,它就可以正常工作,但这当然会将事件应用于我的所有“ta_butt”按钮 - 我只希望它在标记为错误的按钮上。

如果有人对 CSS 感兴趣:

.ta_butt {
color: white;
border-radius: 7px;
font-size:150%;
text-align:center;
float: left;
margin: 1px;
width: 100%;
padding: 1px;
cursor: pointer;
cursor: hand;
background: -webkit-linear-gradient(top, #0066FF, #4D94FF); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(bottom, #0066FF, #4D94FF); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(bottom, #0066FF, #4D94FF); /* For Firefox 3.6 to 15 */
background: linear-gradient(to bottom, #0066FF, #4D94FF); /* Standard syntax (must be last) */
}

.ta_butt.error {
background: -webkit-linear-gradient(top, #CC0000, #DB4D4D); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(bottom, #CC0000, #DB4D4D); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(bottom, #CC0000, #DB4D4D); /* For Firefox 3.6 to 15 */
background: linear-gradient(to bottom, #CC0000, #DB4D4D); /* Standard syntax (must be last) */
}

.ta_butt.error:hover {
background: -webkit-linear-gradient(top, #DB4D4D, #CC0000); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(bottom, #DB4D4D, #CC0000); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(bottom, #DB4D4D, #CC0000); /* For Firefox 3.6 to 15 */
background: linear-gradient(to bottom, #DB4D4D, #CC0000); /* Standard syntax (must be last) */
}

最佳答案

对于多个类,在类名之间使用空格:

var ta_butt = document.getElementsByClassName("ta_butt error");

关于javascript - 将事件监听器添加到内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32115548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com