最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery filter image src issues - Stack Overflow

programmeradmin2浏览0评论

if the image contains NO src then i want to hide the div.PageHeaderDescription

<div class="PageHeaderDescription">
  <h1>Bistro 300</h1>
    <img border="0" src="images/Category/large/469.jpg" id="EntityPic621">
    This is some text
</div>

To do this i used:

if ($("div.PageHeaderDescription img").filter("[src='']")) {
    $("div.PageHeaderDescription").hide();
}

however if you put a src path into the image the jquery still hides div.PageHeaderDescription which is wrong. It needs to be visable if there is an image src.

Here is my example: /

if the image contains NO src then i want to hide the div.PageHeaderDescription

<div class="PageHeaderDescription">
  <h1>Bistro 300</h1>
    <img border="0" src="images/Category/large/469.jpg" id="EntityPic621">
    This is some text
</div>

To do this i used:

if ($("div.PageHeaderDescription img").filter("[src='']")) {
    $("div.PageHeaderDescription").hide();
}

however if you put a src path into the image the jquery still hides div.PageHeaderDescription which is wrong. It needs to be visable if there is an image src.

Here is my example: http://jsfiddle/dfasd/1/

Share Improve this question asked Jul 19, 2012 at 13:56 SOLDIER-OF-FORTUNESOLDIER-OF-FORTUNE 1,6545 gold badges39 silver badges68 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4
$("div.PageHeaderDescription img[src='']").parent().hide();

Find img with empty src and hide its parent div.PageHeaderDescription;

DEMO

OR

$("div.PageHeaderDescription").has("img[src='']").hide();

Hide this div.PageHeaderDescription which has img with empty src.

DEMO

filter() returns a jQuery object, which is always truthy, whether or not elements were matched.

What you should be doing instead is checking the lengthproperty of the object returned;

if ($("div.PageHeaderDescription img").filter("[src='']").length) {
    $("div.PageHeaderDescription").hide();
}

Although you could shorten this to;

if ($("div.PageHeaderDescription img[src='']").length) {
    $("div.PageHeaderDescription").hide();
}

But if you have multiple div.PageHeaderDescription on the page, you should be doing;

$("div.PageHeaderDescription").each(function () {
    var self = $(this);

    if (self.find('img[src=""]').length) {
        self.hide();
    }
});

If you want to do that when page loads, you can use this code:

$("div.PageHeaderDescription img")     // search for all img's inside a div...
   .filter("[src='']")                 // get only ones with no src
   .parents("div.PageHeaderDescription")   // get their parent elements
   .hide(); // hide then

Or you can run the same script every time you want to check if any img with no src is in page and must be hidden.

No need to make it more plicated than it needs to be with a .filter().

var phd = $("div.PageHeaderDescription");    
if ($('img:not([src])',phd).length || $('img[src=""]',phd).length) {
    phd.hide();
}

I check to see if the img doesn't have the src attribute or if it does have the attribute I check if it is blank.

Fiddle: http://jsfiddle/iambriansreed/G6bPu/

As already said, a jQuery object always evaluates to true. But you don't need an if statement at all, simply reduce the set of elements to those that have an "empty" image. That's what .filter() is there for:

$("div.PageHeaderDescription").filter(function() {
    return $(this).find("img[src='']").length > 0;
}).hide();
发布评论

评论列表(0)

  1. 暂无评论