如何定位图像然后单击它并执行删除功能jquery?

时间:2015-02-18 21:03:56

标签: php jquery

点击http://jsfiddle.net/4y1b1j8g/25/查看结果。我想将十字标记移动到右侧,不会出现在文本上方。我希望它像Stackoverflow标签。然后单击图像并将其删除。我尝试使用$(removePic).click.(function() { $(removePic).remove()}):,但它无效。



var removePic = $('.test').css({'background-image' : 'url(https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/16x16/DeleteRed.png)',
'background-repeat' : 'no-repeat',
'background-position' : 'right'})

.test {
    display: inline-block;
    margin: 0 3px 0 0;
    border:solid;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="test" id="test0">dog</div>
<div class="test" id="test1">lion</div>
<div class="test" id="test2">cat</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

$(document).ready(function(){
    $(".test").append('<img src="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/16x16/DeleteRed.png">');   
    $(document).on("click", ".test img", function() {
    $(this).parent().remove();
    });
});

工作Fiddle