[英]edit css to event.target
I am trying to make background change CSS attribute to event.target
and it is not working. 我正在尝试将后台更改CSS属性设置为event.target
并且它无法正常工作。 All the commands are working except changing event.target.css 除了更改event.target.css之外,所有命令都正常工作
Here is the code I'm using: 这是我正在使用的代码:
$(document).ready(function() {
$(".plusMatch").click(function(event) {
if ($("#productImage"+event.target.id).hasClass("visible")) {
$("#productImage"+event.target.id).css("display", "none");
$("#productImage"+event.target.id).removeClass('visible');
event.target.css("background", "url(minusSign.jpg)");
} else {
$("#productImage"+event.target.id).css("display", "block");
$("#productImage"+event.target.id).addClass('visible');
event.target.css("background", "url(minusSign.jpg)");
}
});
});
The first thing is that you are using JQuery in your project, so it is better for you to write $(event.target)
instead of event.target
if you want to use the JQuery CSS property . 第一件事是您在项目中使用JQuery,因此如果您想使用JQuery CSS属性 ,最好编写$(event.target)
而不是event.target
。
Then, to change any CSS property with javascript, you have two options : 然后,要使用javascript更改任何CSS属性,您有两个选择:
The native style property will edit the style tag on your HTML element, for example : 本机样式属性将编辑HTML元素上的样式标记,例如:
event.target.style.background = 'url(myPicture.jpg)';
The JQuery css property JQuery css属性
$(event.target).css('background': 'url(myPicture.jpg)');
Both of these solutions will produce the following result : 这两种解决方案都会产生以下结果:
<div style="background:url('myPicture.jpg');"></div>
event.target
is a DOM element, not a jQuery object. event.target
是一个DOM元素,而不是一个jQuery对象。
Either make a jQuery object or use the native DOM .style
API. 要么创建一个jQuery对象,要么使用本机DOM .style
API。
It should be $(event.target).css(...)
because event.target
is DOM element, not a jQuery Object. 它应该是$(event.target).css(...)
因为event.target
是DOM元素,而不是jQuery对象。 Use $ to convert to jQuery Object: 使用$转换为jQuery对象:
$(event.target).css("background", "url(minusSign.jpg)");
Alternatively, use .style
on the regular DOM element: 或者,在常规DOM元素上使用.style
:
event.target.style.background = "url(minusSign.jpg)";
You have to change this line: 你必须改变这一行:
event.target.css("background", "url(minusSign.jpg)");
to: 至:
$(event.target).css("background", "url(minusSign.jpg)");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.