[英]Can you use “this” attribute on onclick of an HTML tag?
Can you use the this
tag for the onclick on an HTML tag? 你可以在HTML标签上使用
this
标签进行onclick吗? Here's my JS code... 这是我的JS代码......
function changeImage() {
this/*<-- right there <--*/.src=a;
}
document.getElementsByTagName('img').onclick = function(){
changeImage();
} ;
Am I doing something wrong? 难道我做错了什么?
Use it this way... 用这种方式......
function changeImage(curr) {
console.log(curr.src);
}
document.getElementsByTagName('img').onclick = function(){
changeImage(this);
} ;
You could use the .call()
method to invoke the function with the context of this
. 您可以使用
.call()
方法用的情况下调用该函数this
。
In this case, you would use: 在这种情况下,您将使用:
changeImage.call(this)
function changeImage() {
this.src = 'http://placehold.it/200/f00';
}
document.getElementsByTagName('img')[0].onclick = function(){
changeImage.call(this);
};
As a side note, getElementsByTagName
returns a live HTMLCollection of elements. 作为旁注,
getElementsByTagName
返回元素的实时HTMLCollection 。 You need to apply the onclick
handler to an element within that collection. 您需要将
onclick
处理程序应用于该集合中的元素。
If you want to apply the event listener to the collection of elements, you iterate through them and add event listeners like this: 如果要将事件侦听器应用于元素集合,则迭代它们并添加如下事件侦听器:
function changeImage() {
this.src = 'http://placehold.it/200/f00';
}
Array.prototype.forEach.call(document.getElementsByTagName('img'), function(el, i) {
el.addEventListener('click', changeImage);
});
Or you could simplify it: 或者你可以简化它:
Array.prototype.forEach.call(document.getElementsByTagName('img'), function(el, i) {
el.addEventListener('click', function () {
this.src = 'http://placehold.it/200/f00';
});
});
You are doing two things wrong. 你做错了两件事。
changeImage
without any context (so this
will be undefined
or window
depending on if you are in strict mode or now). changeImage
(因此, this
将是undefined
或window
具体取决于您是处于严格模式还是现在)。 A fixed version would look like this: 固定版本看起来像这样:
var images = document.getElementsByTagName('img');
for (var i = 0; i < images.length; i++) {
images[i].onclick = function () {
changeImage.call(this);
};
}
But a tidier version would skip the anonymous function that does nothing except call another function: 但是更整洁的版本会跳过除了调用另一个函数之外什么都不做的匿名函数:
var images = document.getElementsByTagName('img');
for (var i = 0; i < images.length; i++) {
images[i].onclick = changeImage;
}
And modern code would use addEventListener
. 现代代码将使用
addEventListener
。
var images = document.getElementsByTagName('img');
for (var i = 0; i < images.length; i++) {
images[i].addEventListener('click', changeImage);
}
However, images are not interactive controls. 但是,图像不是交互式控件。 You can't (by default) focus them, so this approach would make them inaccessible to people who didn't use a pointing device.
您不能(默认情况下)将它们集中在一起,因此这种方法会使那些没有使用指针设备的人无法访问它们。 Better to use controls that are designed for interaction in the first place.
最好使用专为交互而设计的控件。
Generally, this should be a plain button. 通常,这应该是一个简单的按钮。 You can use CSS to remove the default padding / border / background.
您可以使用CSS删除默认填充/边框/背景。
If you can't put a button in your plain HTML, you can add it with JS. 如果您不能在纯HTML中添加按钮,则可以使用JS添加它。
var images = document.getElementsByTagName('img');
for (var i = 0; i < images.length; i++) {
var image = images[i];
var button = document.createElement('button');
button.type = "button";
image.parentNode.replaceChild(button, image);
button.appendChild(image);
button.addEventListener('click', changeImage);
}
function changeImage(event) {
this.firstChild.src = a;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.