[英]click event not firing on anchor tag
My click event is not firing. 我的点击事件未触发。
My HTML : 我的HTML :
<tr>
<td>121345</td>
<!-- Other td-elements -->
<td><a href = "#" class = "remove-item">Remove</a></td>
</tr>
My Javascript : 我的Java语言 :
window.document.load = function()
{
document.getElementsByClassName('remove-item').onClick(function(element)
{
console.log("element");
var productId = element.parentElement.parentElement.childElements[0].value;
console.log(productId);
});
};
I don't want to use jQuery for this. 我不想为此使用jQuery。
The problem is that getElementsByClassName
returns a collection elements - not a single one because classes can be applied to multiple elements. 问题在于getElementsByClassName
返回一个集合元素-不是单个元素,因为类可以应用于多个元素。 From the documentation: 从文档中:
Returns an array-like object of all child elements which have all of the given class names. 返回具有所有给定类名称的所有子元素的类似数组的对象。
Just use [0]
to access the first element in the return array-like object (the first element with that class). 只需使用[0]
访问类似返回数组的对象中的第一个元素(该类的第一个元素)。
Also onClick
is not the correct way to add an event handler. 另外, onClick
也不是添加事件处理程序的正确方法。 You may use addEventListener
which takes in an event and a handler: 您可以使用addEventListener
,它接受一个事件和一个处理程序:
window.document.load = function() {
document.getElementsByClassName('remove-item')[0].addEventListener("click", function(element) { //add click handler to first element with remove-item class
console.log("element");
var productId = element.parentElement.parentElement.childElements[0].value;
console.log(productId);
});
}
If your intention is to apply a click handler to each of the elements with the event, you may use Array.prototype.forEach
: 如果您打算将单击处理程序应用于具有该事件的每个元素,则可以使用Array.prototype.forEach
:
var elements = document.getElementsByClassName('remove-item');
Array.prototype.forEach.call(elements, function(currentElement) {
console.log("element");
var productId = currentElement.parentElement.parentElement.childElements[0].value;
console.log(productId);
});
forEach
iterates over each element of the array-like object and for each element (stored in currentElement
) will execute the callback. forEach
遍历类数组对象的每个元素,并为每个元素(存储在currentElement
)执行回调。
You did some errors there: 您在此处犯了一些错误:
document.getElementsByClassName
returns multiple elements document.getElementsByClassName
返回多个元素 .onClick(...)
is not the way to add an onclick handler .onClick(...)
不是添加onclick处理程序的方法 This snippet will remove the parenting <tr>
element on click of a Remove
-button: 单击“ Remove
按钮后,此代码片段将删除育儿<tr>
元素:
var items = document.getElementsByClassName('remove-item'); for(var i = 0; i < items.length; i++) { var item = items[i]; item.onclick = function(e) { e.target.parentNode.parentNode.style.display = "none"; }; }
<table> <tr> <td>Test 1</td> <td><a href="#" class="remove-item">Remove</a></td> </tr> <tr> <td>Test 2</td> <td><a href="#" class="remove-item">Remove</a></td> </tr> <tr> <td>Test 3</td> <td><a href="#" class="remove-item">Remove</a></td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.