จะหารหัสของปุ่มที่ถูกคลิกได้อย่างไร
<button id="1" onClick="reply_click()"></button>
<button id="2" onClick="reply_click()"></button>
<button id="3" onClick="reply_click()"></button>
function reply_click()
{
}
จะหารหัสของปุ่มที่ถูกคลิกได้อย่างไร
<button id="1" onClick="reply_click()"></button>
<button id="2" onClick="reply_click()"></button>
<button id="3" onClick="reply_click()"></button>
function reply_click()
{
}
คำตอบ:
คุณต้องส่ง ID เป็นพารามิเตอร์ฟังก์ชัน ทำเช่นนี้:
<button id="1" onClick="reply_click(this.id)">B1</button>
<button id="2" onClick="reply_click(this.id)">B2</button>
<button id="3" onClick="reply_click(this.id)">B3</button>
<script type="text/javascript">
function reply_click(clicked_id)
{
alert(clicked_id);
}
</script>
นี่จะส่ง ID this.id
ตามclicked_id
ที่คุณสามารถใช้ในการทำงานของคุณ ดูการทำงานจริงได้ที่นี่
event.target.id
(สำหรับฉันทั้ง Firefox และ IE กำลังขว้างมัน) นี่เป็นทางออกที่ยอดเยี่ยมที่ใช้งานได้กับเบราว์เซอร์หลักทั้งสาม
โดยทั่วไปสิ่งต่างๆจะง่ายต่อการจัดระเบียบหากคุณแยกรหัสและมาร์กอัปของคุณ กำหนดองค์ประกอบทั้งหมดของคุณจากนั้นในส่วน JavaScript ของคุณให้กำหนดการกระทำต่าง ๆ ที่ควรดำเนินการกับองค์ประกอบเหล่านั้น
เมื่อมีการเรียกใช้ตัวจัดการเหตุการณ์มันจะถูกเรียกภายในบริบทขององค์ประกอบที่คลิก ดังนั้นตัวระบุสิ่งนี้จะอ้างถึงองค์ประกอบ DOM ที่คุณคลิก จากนั้นคุณสามารถเข้าถึงแอตทริบิวต์ขององค์ประกอบผ่านตัวระบุนั้น
ตัวอย่างเช่น:
<button id="1">Button 1</button>
<button id="2">Button 2</button>
<button id="3">Button 3</button>
<script type="text/javascript">
var reply_click = function()
{
alert("Button clicked, id "+this.id+", text"+this.innerHTML);
}
document.getElementById('1').onclick = reply_click;
document.getElementById('2').onclick = reply_click;
document.getElementById('3').onclick = reply_click;
</script>
ใช้ JAVASCRIPT บริสุทธิ์: ฉันรู้ว่ามันสาย แต่อาจเป็นสำหรับคนในอนาคตมันสามารถช่วย:
ในส่วน HTML:
<button id="1" onClick="reply_click()"></button>
<button id="2" onClick="reply_click()"></button>
<button id="3" onClick="reply_click()"></button>
ในตัวควบคุม Javascipt:
function reply_click()
{
alert(event.srcElement.id);
}
วิธีนี้เราไม่ต้องผูก 'id' ขององค์ประกอบในขณะที่เรียกใช้ฟังก์ชันจาวาสคริปต์
this
พารามิเตอร์เป็นonClick
( ในความเป็นจริงไม่ได้ใช้ onClick แต่ onChange นั่นอาจเป็นปัญหาหรือไม่) ฉันได้ตรวจสอบประมาณเล็กน้อยและดูเหมือนว่าจะมีความสับสนมากเกี่ยวกับevent
ตัวแปรนี้ที่เกิดขึ้น - เป็นพารามิเตอร์ "โดยนัย" หรือต้องระบุอย่างชัดเจนว่าเป็นอาร์กิวเมนต์ (เช่นfunction reply_click(event)
ที่ฉันเคยเห็นด้วยใน บางแห่ง) จะใช้ได้เฉพาะเมื่อมอบหมายผู้ฟังเหตุการณ์ผ่านaddEventListener
... ฉันเล่นไปรอบ ๆ แต่ไม่สามารถทำงานได้
(ฉันคิดว่าid
แอตทริบิวต์ต้องเริ่มต้นด้วยตัวอักษรอาจผิด)
คุณสามารถไปสำหรับการมอบหมายกิจกรรม ...
<div onClick="reply_click()">
<button id="1"></button>
<button id="2"></button>
<button id="3"></button>
</div>
function reply_click(e) {
e = e || window.event;
e = e.target || e.srcElement;
if (e.nodeName === 'BUTTON') {
alert(e.id);
}
}
... แต่นั่นต้องให้คุณรู้สึกสบายใจกับโมเดลเหตุการณ์ที่แปลกประหลาด
e
อาร์กิวเมนต์จะถูกสร้างขึ้นโดยอัตโนมัติ ถ้ามันไม่ได้แล้วเราจะต้องจัดการกับ IE6-8 window.event
ซึ่งแทนที่จะแสดงให้เห็นว่าวัตถุที่มีประโยชน์ผ่านทาง
<button id="1" onClick="reply_click(this)"></button>
<button id="2" onClick="reply_click(this)"></button>
<button id="3" onClick="reply_click(this)"></button>
function reply_click(obj)
{
var id = obj.id;
}
<button id="1" class="clickMe"></button>
<button id="2" class="clickMe"></button>
<button id="3" class="clickMe"></button>
<script>
$('.clickMe').click(function(){
alert(this.id);
});
</script>
โดยทั่วไปจะแนะนำให้หลีกเลี่ยง JavaScript แบบอินไลน์ แต่ไม่ค่อยมีตัวอย่างของวิธีการทำ
นี่คือวิธีการแนบกิจกรรมกับปุ่มของฉัน
ฉันไม่พอใจอย่างสิ้นเชิงกับวิธีการที่แนะนำนานกว่าเมื่อเทียบกับonClick
แอตทริบิวต์ แบบง่าย
<button class="btn">Button</button>
<script>
let OnEvent = (doc) => {
return {
on: (event, className, callback) => {
doc.addEventListener('click', (event)=>{
if(!event.target.classList.contains(className)) return;
callback.call(event.target, event);
}, false);
}
}
};
OnEvent(document).on('click', 'btn', function (e) {
window.console.log(this, e);
});
</script>
<!DOCTYPE html>
<html>
<head>
<script>
(function(doc){
var hasClass = function(el,className) {
return el.classList.contains(className);
}
doc.addEventListener('click', function(e){
if(hasClass(e.target, 'click-me')){
e.preventDefault();
doSomething.call(e.target, e);
}
});
})(document);
function insertHTML(str){
var s = document.getElementsByTagName('script'), lastScript = s[s.length-1];
lastScript.insertAdjacentHTML("beforebegin", str);
}
function doSomething(event){
console.log(this.id); // this will be the clicked element
}
</script>
<!--... other head stuff ...-->
</head>
<body>
<!--Best if you inject the button element with javascript if you plan to support users with javascript disabled-->
<script>
insertHTML('<button class="click-me" id="btn1">Button 1</button>');
</script>
<!--Use this when you don't care about broken buttons when javascript is disabled.-->
<!--buttons can be used outside of forms https://stackoverflow.com/a/14461672/175071 -->
<button class="click-me" id="btn2">Button 2</button>
<input class="click-me" type="button" value="Button 3" id="btn3">
<!--Use this when you want to lead the user somewhere when javascript is disabled-->
<a class="click-me" href="/path/to/non-js/action" id="btn4">Button 4</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
(function(doc){
var cb_addEventListener = function(obj, evt, fnc) {
// W3C model
if (obj.addEventListener) {
obj.addEventListener(evt, fnc, false);
return true;
}
// Microsoft model
else if (obj.attachEvent) {
return obj.attachEvent('on' + evt, fnc);
}
// Browser don't support W3C or MSFT model, go on with traditional
else {
evt = 'on'+evt;
if(typeof obj[evt] === 'function'){
// Object already has a function on traditional
// Let's wrap it with our own function inside another function
fnc = (function(f1,f2){
return function(){
f1.apply(this,arguments);
f2.apply(this,arguments);
}
})(obj[evt], fnc);
}
obj[evt] = fnc;
return true;
}
return false;
};
var hasClass = function(el,className) {
return (' ' + el.className + ' ').indexOf(' ' + className + ' ') > -1;
}
cb_addEventListener(doc, 'click', function(e){
if(hasClass(e.target, 'click-me')){
e.preventDefault ? e.preventDefault() : e.returnValue = false;
doSomething.call(e.target, e);
}
});
})(document);
function insertHTML(str){
var s = document.getElementsByTagName('script'), lastScript = s[s.length-1];
lastScript.insertAdjacentHTML("beforebegin", str);
}
function doSomething(event){
console.log(this.id); // this will be the clicked element
}
</script>
<!--... other head stuff ...-->
</head>
<body>
<!--Best if you inject the button element with javascript if you plan to support users with javascript disabled-->
<script type="text/javascript">
insertHTML('<button class="click-me" id="btn1">Button 1</button>');
</script>
<!--Use this when you don't care about broken buttons when javascript is disabled.-->
<!--buttons can be used outside of forms https://stackoverflow.com/a/14461672/175071 -->
<button class="click-me" id="btn2">Button 2</button>
<input class="click-me" type="button" value="Button 3" id="btn3">
<!--Use this when you want to lead the user somewhere when javascript is disabled-->
<a class="click-me" href="/path/to/non-js/action" id="btn4">Button 4</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
(function($){
$(document).on('click', '.click-me', function(e){
doSomething.call(this, e);
});
})(jQuery);
function insertHTML(str){
var s = document.getElementsByTagName('script'), lastScript = s[s.length-1];
lastScript.insertAdjacentHTML("beforebegin", str);
}
function doSomething(event){
console.log(this.id); // this will be the clicked element
}
</script>
<!--... other head stuff ...-->
</head>
<body>
<!--Best if you inject the button element with javascript if you plan to support users with javascript disabled-->
<script type="text/javascript">
insertHTML('<button class="click-me" id="btn1">Button 1</button>');
</script>
<!--Use this when you don't care about broken buttons when javascript is disabled.-->
<!--buttons can be used outside of forms https://stackoverflow.com/a/14461672/175071 -->
<button class="click-me" id="btn2">Button 2</button>
<input class="click-me" type="button" value="Button 3" id="btn3">
<!--Use this when you want to lead the user somewhere when javascript is disabled-->
<a class="click-me" href="/path/to/non-js/action" id="btn4">Button 4</a>
</body>
</html>
คุณสามารถเรียกใช้สิ่งนี้ก่อนที่เอกสารจะพร้อมคลิกที่ปุ่มจะใช้งานได้เพราะเราแนบกิจกรรมเข้ากับเอกสาร
นี่คือjsfiddle
ด้วยเหตุผลแปลก ๆinsertHTML
ฟังก์ชั่นนี้ใช้ไม่ได้ถึงแม้ว่ามันจะทำงานได้ในทุกเบราว์เซอร์ของฉัน
คุณสามารถแทนที่insertHTML
ด้วยdocument.write
หากคุณไม่คำนึงถึงข้อเสียเปรียบ
<script>
document.write('<button class="click-me" id="btn1">Button 1</button>');
</script>
แหล่งที่มา:
หากคุณไม่ต้องการส่งอาร์กิวเมนต์ใด ๆ ไปยังฟังก์ชัน onclick เพียงใช้event.target
เพื่อรับองค์ประกอบที่ถูกคลิก:
<button id="1" onClick="reply_click()"></button>
<button id="2" onClick="reply_click()"></button>
<button id="3" onClick="reply_click()"></button>
function reply_click()
{
// event.target is the element that is clicked (button in this case).
console.log(event.target.id);
}
ด้วยจาวาสคริปต์ที่บริสุทธิ์คุณสามารถทำสิ่งต่อไปนี้:
var buttons = document.getElementsByTagName("button");
var buttonsCount = buttons.length;
for (var i = 0; i < buttonsCount; i += 1) {
buttons[i].onclick = function(e) {
alert(this.id);
};
}
ตรวจสอบบนJsFiddle
คุณสามารถทำได้ด้วยวิธีนี้:
<input type="button" id="1234" onclick="showId(this.id)" value="click me to show my id"/>
<script type="text/javascript">
function showId(obj) {
var id=obj;
alert(id);
}
id=obj;
และเพียงแค่มีalert(obj);
<button id="1" onClick="reply_click()"></button>
<button id="2" onClick="reply_click()"></button>
<button id="3" onClick="reply_click()"></button>
function reply_click()
{
console.log(window.event.target.id)
}
<button id="1"class="clickMe"></button>
<button id="2" class="clickMe"></button>
<button id="3" class="clickMe"></button>
$('.clickMe').live('click',function(){
var clickedID = this.id;
});
ปุ่ม 1 ปุ่ม 2 ปุ่ม 3
var reply_click = function() {
alert("Button clicked, id "+this.id+", text"+this.innerHTML);
}
document.getElementById('1').onclick = reply_click;
document.getElementById('2').onclick = reply_click;
document.getElementById('3').onclick = reply_click;
ขออภัยเป็นคำตอบที่ช้า แต่จริง ๆ ถ้าคุณทำอย่างนี้: -
$(document).ready(function() {
$('button').on('click', function() {
alert (this.id);
});
});
นี่จะได้รับ ID ของปุ่มใด ๆ ที่คลิก
หากคุณต้องการได้รับมูลค่าของปุ่มคลิกในสถานที่หนึ่งให้ใส่ไว้ในคอนเทนเนอร์เช่น
<div id = "myButtons"> buttons here </div>
และเปลี่ยนรหัสเป็น: -
$(document).ready(function() {
$('.myButtons button').on('click', function() {
alert (this.id);
});
});
ฉันหวังว่านี่จะช่วยได้
นี่จะบันทึกรหัสขององค์ประกอบที่คลิก: addFields
<button id="addFields" onclick="addFields()">+</button>
<script>
function addFields(){
console.log(event.toElement.id)
}
</script>