Javascript JQuery Image OnClick 添加边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8682755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:03:31  来源:igfitidea点击:

JQuery Image OnClick Add Border

javascriptjquerycss

提问by Satch3000

I need some JQuery code so that I can add a CSS border to an image when I click on it.

我需要一些 JQuery 代码,以便在单击图像时可以向图像添加 CSS 边框。

<img src="myimage" onclick="AddBorder();" />

回答by PriorityMark

If you're using jQuery, I'd recommend removing the inline onclick handler, as that's the less desirable way to bind to the event:

如果您使用 jQuery,我建议删除内联 onclick 处理程序,因为这是绑定到事件的不太理想的方式:

<img src="http://jsfiddle.net/img/logo.png" />

And then just setup a simple event binder:

然后只需设置一个简单的事件绑定器:

$(function () {
  $("img").click(function() {
    $(this).css('border', "solid 2px red");  
  });
});

Here's a jsfiddle

这是一个jsfiddle

回答by Jason MacLean

In that onClick, instead of AddBorder();, place:

在那个 onClick 中,而不是 AddBorder();,放置:

$(this).css("border","1px solid black");

回答by Matt H

Er... what have you tried so far?

呃……到目前为止你尝试了什么?

function AddBorder() { $(this).css('border','1px solid red'); }