js 中onclick 事件 點擊后指向自己的對象,查找或者添加屬性 用關鍵字this 傳入參數 (可以改變原標簽css)


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.div1{
color: #0000cc;
font-size: 20px;
}
</style>
</head>

<body>
<div>
<div onclick="func(this)" name="book1">hello 1</div>
<div onclick="func(this)" name="book2">hello 2</div>
<div onclick="func(this)" name="book3">hello 3</div>
<div onclick="func(this)" name="book4">hello 4</div>
</div>
<script> //onclick 綁定的函數 傳參數 關鍵字this
function func(a) { // 點擊后直接獲取本點擊對象
alert(a.getAttribute("name")); // 只能用getattribute 可以獲得對象中的各種屬性 name
alert(a.innerHTML); // 獲取文本內容

a.setAttribute("class","div1") // 同樣也可以給點擊事件的對象 加入屬性 class="div1"
} // div1 原來並沒有加入 點擊后 改變了字體大小顏色
</script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM