单击" X"删除图像标记在图像的右上方

时间:2016-10-21 12:42:30

标签: javascript html css angularjs

我想通过点击" X"删除图像。在图像的右上方标记,我在CSS http://jsfiddle.net/yHNEv/后面跟着这个小提琴。

HTML code:

 <div class="img-wrap">
  <span  ng-click="deleteLocalfile()" class="close">&times;</span>
  <a id="div2" href><img ng-src="{{imagepreviewUrl}}" style="width: 100px; height: 100px;" alt=""></a>
</div>

控制器代码:

 $scope.deleteLocalfile = function(){

  var result = document.getElementsByClassName("close");
  var wrappedResult = angular.element(result);

  wrappedResult.remove();

}

上述代码删除&#34; X&#34;点击它时标记,而不是图像,但是点击&#34; X&#34;我正在这里举办活动。

第二种方法:

 <div class="img-wrap">
  <span   class="close">&times;</span>
  <a  ng-click="deleteLocalfile()" id="div2" href><img ng-src="{{imagepreviewUrl}}" style="width: 100px; height: 100px;" alt=""></a>
</div>

第二种方法控制器代码

$scope.deleteLocalfile = function(){
var result = document.getElementsById("div2");
 var wrappedResult = angular.element(result);
 wrappedResult.remove();
}

在这里,我没有点击&#34; X&#34;标记,在图像上获取事件单击仅删除图像保留&#34; X&#34;标记

3 个答案:

答案 0 :(得分:1)

这基本上是Sasikumar的回答,改写为不使用jQuery。

var closeBtns = document.querySelectorAll('.img-wrap .close')

for (var i = 0, l = closeBtns.length; i < l; i++) {
  closeBtns[i].addEventListener('click', function() {
    var imgWrap = this.parentElement;
    imgWrap.parentElement.removeChild(imgWrap);
  });
}
.img-wrap {
  position: relative;
  display: inline-block;
  border: 1px red solid;
  font-size: 0;
}
.img-wrap .close {
  position: absolute;
  top: 2px;
  right: 2px;
  z-index: 100;
  background-color: #FFF;
  padding: 5px 2px 2px;
  color: #000;
  font-weight: bold;
  cursor: pointer;
  opacity: .2;
  text-align: center;
  font-size: 22px;
  line-height: 10px;
  border-radius: 50%;
}
.img-wrap:hover .close {
  opacity: 1;
}
<div class="img-wrap">
  <span class="close">&times;</span>
  <img src="http://images.freeimages.com/images/premium/previews/2282/2282459-fisheye-tank.jpg" width="200" data-id="123">
</div>

<div class="img-wrap">
  <span class="close">&times;</span>
  <img src="http://images.freeimages.com/images/previews/38a/kendo-armor-3-1431999.jpg" width="100" data-id="103">
</div>

JSFiddle,如果是首选:https://jsfiddle.net/TheQueue841/0xugckje/

答案 1 :(得分:0)

试试此代码

$('.img-wrap .close').on('click', function() {
    $(this).closest('.img-wrap').remove();
});
.img-wrap {
    position: relative;
    display: inline-block;
    border: 1px red solid;
    font-size: 0;
}
.img-wrap .close {
    position: absolute;
    top: 2px;
    right: 2px;
    z-index: 100;
    background-color: #FFF;
    padding: 5px 2px 2px;
    color: #000;
    font-weight: bold;
    cursor: pointer;
    opacity: .2;
    text-align: center;
    font-size: 22px;
    line-height: 10px;
    border-radius: 50%;
}
.img-wrap:hover .close {
    opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="img-wrap">
<span class="close">&times;</span>
<img src="http://images.freeimages.com/images/premium/previews/2282/2282459-fisheye-tank.jpg" width="200" data-id="123">
</div>

<div class="img-wrap">
<span class="close">&times;</span>
<img src="http://images.freeimages.com/images/previews/38a/kendo-armor-3-1431999.jpg" width="100" data-id="103">
</div>

jsfiddle demo http://jsfiddle.net/yHNEv/356/

答案 2 :(得分:0)

更改此css:

.img-wrap {
    position: relative;
    display: inline-block;
    border: 1px red solid;
    font-size: 0;
}

.img-wrap .close {
    position: absolute;
    top: 0px;
    right: 0px;
    z-index: 100;
    cursor: pointer;
    opacity: 1;
    height: 100%;
    width: 100%;
}