简体   繁体   English

你可以在HTML标签的onclick上使用“this”属性吗?

[英]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)

Example Here 这里的例子

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: 如果要将事件侦听器应用于元素集合,则迭代它们并添加如下事件侦听器:

Updated Example 更新的示例

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: 或者你可以简化它:

Example Here 这里的例子

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. 你做错了两件事。

  1. You are assigning the event handler to a NodeList instead of to an element (or set of elements) 您将事件处理程序分配给NodeList而不是元素(或元素集)
  2. You are calling changeImage without any context (so this will be undefined or window depending on if you are in strict mode or now). 您在没有任何上下文的情况下调用changeImage (因此, this将是undefinedwindow具体取决于您是处于严格模式还是现在)。

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.

相关问题 如何使用onclick HTML属性调用JavaScript中的函数? - How can I use an onclick HTML attribute to call a function in JavaScript? 我可以动态获取HTML元素的onclick属性并在javascript中使用它吗? - Can I dynamically take a Html element onclick attribute and use it in javascript? 无法从HTML标记属性“值”到onClick脚本获取值 - Can't get value from HTML tag attribute “value” to onClick script 您可以在JS字符串打印的HTML标签上使用jQuery选择器吗? - Can you use a jQuery selector on a HTML tag that a JS string printed? 您可以在 HTML 脚本标签上同时使用 async 和 defer 属性吗? - Can you use both the async and defer attributes on a HTML script tag? 您可以将哪些参数传递给html属性onchange,onclick等? - What parameters can you pass to html attribute onchange, onclick, etc.? 如果在单个HTML文档中多次使用具有相同“src”属性的<script>标记会发生什么? - What happens if you use a <script> tag with the same “src” attribute multiple times within a single HTML document? 使用jQuery动态地将HTML标签(包括“ onclick”属性)附加到页面上 - Append an HTML tag, including an `onclick` attribute, to a page dynamically with jQuery 使用onclick属性中的Javascript在html中获取子元素标记 - Get a child element tag in html using Javascript in the onclick attribute HTML锚标记的onclick属性不会调用javascript函数 - HTML anchor tag's onclick attribute does not call javascript function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM