ล่าสุดการพัฒนาเว็บบทเรียน
×

JavaScript การอ้างอิง

ภาพรวม

JavaScript

JS เชือก JS จำนวน JS ผู้ประกอบการ JS งบ JS คณิตศาสตร์ JS วันที่ JS แถว JS บูลีน JS นิพจน์ทั่วไป JS สถานการณ์โดยรวม JS การแปลง

เบราว์เซอร์ BOM

Window Navigator Screen History Location

HTML DOM

DOM เอกสาร DOM องค์ประกอบ DOM แอตทริบิวต์ DOM เหตุการณ์ DOM รูปแบบ

HTML วัตถุ

<a> <abbr> <address> <area> <article> <aside> <audio> <b> <base> <bdo> <blockquote> <body> <br> <button> <canvas> <caption> <cite> <code> <col> <colgroup> <datalist> <dd> <del> <details> <dfn> <dialog> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <footer> <form> <head> <header> <h1> - <h6> <hr> <html> <i> <iframe> <img> <ins> <input> button <input> checkbox <input> color <input> date <input> datetime <input> datetime-local <input> email <input> file <input> hidden <input> image <input> month <input> number <input> password <input> radio <input> range <input> reset <input> search <input> submit <input> text <input> time <input> url <input> week <kbd> <keygen> <label> <legend> <li> <link> <map> <mark> <menu> <menuitem> <meta> <meter> <nav> <object> <ol> <optgroup> <option> <output> <p> <param> <pre> <progress> <q> <s> <samp> <script> <section> <select> <small> <source> <span> <strong> <style> <sub> <summary> <sup> <table> <td> <th> <tr> <textarea> <time> <title> <track> <u> <ul> <var> <video>

วัตถุอื่น ๆ

CSSStyleDeclaration


 

เหตุการณ์ onclick

อ้างอิงวัตถุที่จัดกิจกรรม วัตถุที่จัดกิจกรรม

ตัวอย่าง

รัน JavaScript เมื่อมีการคลิกปุ่ม:

<button onclick="myFunction()">Click me</button>
ลองตัวเอง»

เพิ่มเติม "Try it Yourself" ตัวอย่างด้านล่าง


ความหมายและการใช้งาน

เหตุการณ์ onclick เกิดขึ้นเมื่อผู้ใช้คลิกที่องค์ประกอบ


สนับสนุนเบราว์เซอร์

เหตุการณ์
onclick ใช่ ใช่ ใช่ ใช่ ใช่

วากยสัมพันธ์

ใน HTML:

ใน JavaScript:

object .onclick=function(){ ลองตัวเอง»

ใน JavaScript โดยใช้ addEventListener() วิธีการ:

object .addEventListener("click", myScript );
ลองตัวเอง»

หมายเหตุ: addEventListener() วิธีการที่ไม่ได้รับการสนับสนุนใน Internet Explorer 8 และรุ่นก่อนหน้านี้


รายละเอียดทางเทคนิค

บับเบิ้ล: ใช่
ยกเลิก: ใช่
ประเภทเหตุการณ์: MouseEvent
แท็ก HTML ที่รองรับ: องค์ประกอบ HTML ทั้งหมดยกเว้น: <ฐาน> <BDO> ฟรี <head> <html> <iframe> <meta> <พารามิเตอร์> <script> <style> และ <title>
DOM เวอร์ชัน: ระดับที่ 2 เหตุการณ์
ตัวอย่าง

ตัวอย่างอื่น ๆ

ตัวอย่าง

คลิกที่ <button> องค์ประกอบที่จะแสดงวันที่ปัจจุบันวันที่และเวลา:

<button onclick="getElementById('demo').innerHTML=Date()">What is the time?</button>
ลองตัวเอง»

ตัวอย่าง

คลิกที่ <p> องค์ประกอบในการเปลี่ยนสีข้อความสีแดง:

<p id="demo" onclick="myFunction()">Click me to change my text color.</p>

<script>
function myFunction() {
    document.getElementById("demo").style.color = "red";
}
</script>
ลองตัวเอง»

ตัวอย่าง

อีกตัวอย่างหนึ่งเกี่ยวกับวิธีการเปลี่ยนสีของที่ <p> องค์ประกอบโดยคลิกที่:

<p id="demo" onclick="myFunction(this, 'red')">Click me to change my text color.</p>

<script>
function myFunction(elmnt,clr) {
    elmnt.style.color = clr;
}
</script>
ลองตัวเอง»

ตัวอย่าง

คลิกที่ปุ่มเพื่อคัดลอกข้อความบางส่วนจากช่องใส่ไปที่ฟิลด์ป้อนอื่น:

<button onclick="myFunction()">Copy Text</button>

<script>
function myFunction() {
    document.getElementById("field2").value = document.getElementById("field1").value;
}
</script>
ลองตัวเอง»

ตัวอย่าง

กำหนด "onclick" เหตุการณ์ไปยังวัตถุหน้าต่าง:

window.onclick = myFunction;

// If the user clicks in the window, set the background color of <body> to yellow
function myFunction() {
    document.getElementsByTagName("BODY")[0].style.backgroundColor = "yellow";
}
ลองตัวเอง»

ตัวอย่าง

ใช้ onclick เพื่อสร้างปุ่มแบบเลื่อนลง:

// Get the button, and when the user clicks on it, execute myFunction
document.getElementById("myBtn").onclick = function() {myFunction()};

/* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
ลองตัวเอง»

หน้าเว็บที่เกี่ยวข้อง

อ้างอิง HTML DOM: เหตุการณ์ ondblclick

อ้างอิง HTML DOM: เหตุการณ์ onmousedown

อ้างอิง HTML DOM: เหตุการณ์ onmouseup


<object เหตุการณ์