难以隐藏没有src的图像

时间:2016-06-10 07:48:14

标签: javascript jquery

我有一系列以下元素:

$('.slidetpl img').each(function () {
        if (this.src.length == 0) {
            $(this).parent().hide();
        }
   });

如果图片没有来源,我想隐藏.slidetpl。为此,我尝试了以下方法:

jQuery

但这不起作用。

有人能指出我正确的方向或告诉我我做错了吗?

4 个答案:

答案 0 :(得分:1)

这对我有效:



// Start an immediate invocable function expression, to auto 
// execute the internal code and isolate the code from the 
// outside JS scope.
(function( $, window, undefined ){

  // Find all the document images and iterate over them. In case you like to
  // to iterate only under slidetpl change the selector from
  // img to .slidetpl img
  $( 'img' ).each(
    function() {

      // If the empty string is equal with the src attribute of 
      // of the current item in the iteration
      if ( '' === $(this).attr('src') ) {

        // Then hide the current item in the iteration.
        $(this).hide();
      }
    }
  );
})( jQuery, this )

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="" />
<img src />
<img src="http://www.iconsdb.com/icons/preview/icon-sets/web-2-orange-2/running-man-xxl.png" />
&#13;
&#13;
&#13;

注意,上面的代码将隐藏所有具有空src属性的图像。如果您只想隐藏.slidetpl的子元素,则可以将选择器从img修改为.slidetpl img

答案 1 :(得分:0)

试试这个:

 $(document).ready(function(){
    $('.slidetpl img[src=""]').parent().hide();
    $('.slidetpl img:not([src=""])').parent().show(); //Can be removed.
});

答案 2 :(得分:0)

<input class="input-number-noarrows" name="ASETid" id="txtHours" 
style="width:100%;height:75px;font-size: 60px;text-align:center;"
focus-me="focusInput" ng-tooltip="Enter hours" ng-model="clone.hoursNoTotal"
ng-change="resetTotalHoursForDelevery(clone.hoursNoTotal)" 
ng-keydown="controlForNumber($event)" type="text" pattern="\d*" step="0.01" />

答案 3 :(得分:0)

你非常接近,只是错过了this的正确语法,请参阅下文:

if ($(this).attr('src').length == 0)

注意演示here