2016-09-14 47 views
1

即时通讯尝试找出如何在点击我的功能时携带按钮的ID。 1函数在鼠标悬停时改变颜色,一种功能在鼠标悬停时将其更改回原始颜色。我知道我可以简单地在CSS中做到这一点,但我只是想学习如何在JavaScript中做到这一点。 在此先感谢。 以下是我的代码。如何检测在javascript中单击了哪个按钮?

var buttonClass = this.className(); 
 
// document.getElementById("mainTitle"); 
 
    this.style.backgroundColor="#000000"; 
 
    this.style.color="#ffffff"; 
 
    this.style.cursor = "pointer"; 
 
} 
 

 
function defaultColor() { 
 
    var buttonClasss = this.getElementById(); 
 
    //document.getElementById("addList"); 
 
    this.style.backgroundColor = "#ffffff"; 
 
    this.style.color = "#000000"; 
 
    this.style.cursor = "pointer"; 
 
} 
 
    
 

 
//event listener for Change Title button 
 
document.getElementById("mainTitle").addEventListener("mouseover", changeColor); 
 
document.getElementById("mainTitle").addEventListener("mouseout", defaultColor); 
 
document.getElementById("mainTitle").addEventListener("click", changeTitle); 
 
//event listener for change title ends here 
 

 
//event listener for add listing 
 
document.getElementById("addList").addEventListener("mouseover", changeColor); 
 
document.getElementById("addList").addEventListener("mouseout", defaultColor); 
 
document.getElementById("addList").addEventListener("click", addListing); 
 
//event listener for add listing ends here
#mainTitle { 
 
    border:1px solid #ff33f4; 
 
    float:left; 
 
    clear:both; 
 
    font-family:arial; 
 
    font-weight:bold; 
 
    font-size:20px; 
 
    border-radius:50px; 
 
    background-color:#ff33ff; 
 
    width:200px; 
 
    height:35px; 
 
    text-align:center; 
 
    padding-top:20px; 
 
    padding-bottom:10px; 
 
    cursor:pointer; 
 
} 
 

 
#addList { 
 
    border:1px solid #ff33f4; 
 
    float:right; 
 
    font-family:arial; 
 
    font-weight:bold; 
 
    font-size:20px; 
 
    border-radius:50px; 
 
    background-color:#ff33ff; 
 
    width:200px; 
 
    height:35px; 
 
    text-align:center; 
 
    padding-top:20px; 
 
    padding-bottom:10px; 
 
    cursor:pointer; 
 
} 
 

 
#main { 
 
    clear:both; 
 
    margin-top:120px; 
 
}

 
    <div id="mainTitle" class="changeTitle">Change Title</div> 
 
    <div id="addList" class="addList">Add List</div>

+0

当功能被触发时,它带有参数'event'。与'事件'对象,你可以得到点击按钮。使用'event.currentTarget'。 –

回答

1

当您将一个函数的事件,你并不真的需要跟踪元素发出该事件的ID,你只需要使用“这个”关键字来访问它。以下是使用示例代码的示例。

<html> 
 
<head> 
 
<style> 
 
#mainTitle { 
 
    border:1px solid #ff33f4; 
 
    float:left; 
 
    clear:both; 
 
    font-family:arial; 
 
    font-weight:bold; 
 
    font-size:20px; 
 
    border-radius:50px; 
 
    background-color:#ff33ff; 
 
    width:200px; 
 
    height:35px; 
 
    text-align:center; 
 
    padding-top:20px; 
 
    padding-bottom:10px; 
 
    cursor:pointer; 
 
} 
 

 
#addList { 
 
    border:1px solid #ff33f4; 
 
    float:right; 
 
    font-family:arial; 
 
    font-weight:bold; 
 
    font-size:20px; 
 
    border-radius:50px; 
 
    background-color:#ff33ff; 
 
    width:200px; 
 
    height:35px; 
 
    text-align:center; 
 
    padding-top:20px; 
 
    padding-bottom:10px; 
 
    cursor:pointer; 
 
} 
 

 
#main { 
 
    clear:both; 
 
    margin-top:120px; 
 
} 
 
</style> 
 
<script type="text/javascript"> 
 

 

 
function defaultColor() { 
 
    //var buttonClasss = this.getElementById(); 
 
    //document.getElementById("addList"); 
 
    this.style.backgroundColor = "#ffffff"; 
 
    this.style.color = "#000000"; 
 
    this.style.cursor = "pointer"; 
 
} 
 
    function changeColor(){ 
 
    this.style.backgroundColor="#000000"; 
 
    this.style.color="#ffffff"; 
 
    this.style.cursor = "pointer"; 
 
    } 
 
    function changeTitle(){ 
 
    } 
 
    function addListing(){ 
 
    } 
 
function OnL(){ 
 
//event listener for Change Title button 
 
document.getElementById("mainTitle").addEventListener("mouseover", changeColor); 
 
document.getElementById("mainTitle").addEventListener("mouseout", defaultColor); 
 
document.getElementById("mainTitle").addEventListener("click", changeTitle); 
 
//event listener for change title ends here 
 

 
//event listener for add listing 
 
document.getElementById("addList").addEventListener("mouseover", changeColor); 
 
document.getElementById("addList").addEventListener("mouseout", defaultColor); 
 
document.getElementById("addList").addEventListener("click", addListing); 
 
} 
 
</script> 
 
</head> 
 
<body onload="OnL()"> 
 

 
    <div id="mainTitle" class="changeTitle">Change Title</div> 
 
    <div id="addList" class="addList">Add List</div> 
 
</body> 
 
</html>

+0

谢谢。这有助于很多 – jerz

+0

最受欢迎... – Owuor

5

每个事件注册的意志带有参数Event

function defaultColor(e) { 
        //^argument (Event) 

    var currentClickedButton = e.currentTarget; // to get the current clicked button 
    /* Your code here */ 
} 
相关问题