0

我在 html 中创建了一个 div 并为其分配了一个背景图像,我想对该照片进行 onclick 事件但不知道如何,那就是 div :

.add_btn {
background-color: #099;
position: absolute;
height: 35px;
width: 200px;
background-image: url(img/add.png); 
background-repeat: no-repeat;
background-position: center center;
top: 265px;
4

2 回答 2

1

You didn't give any information about your DOM except that you have an element with that class. It's hard to give a good answer but I'll try.

The following code will attach a function to the onclick events of all the elements with the add_btn class.

document.getElementsByClassName("add_btn").onclick =
    function () { 
        alert("Hello"); 
    };
于 2013-09-12T20:01:45.687 回答
1

无法单击背景图像。因为你有一个前景层。但是您可以使用此 JQuery 进行前景图像点击...

$('.className > .Inner_Classname2= > img').click(function(e) {
alert("Ok");    
 });
于 2013-09-13T05:32:54.307 回答