检查类是否由 img src 组成,如果没有,则隐藏它.jquery.
check if class consist of img src, if not, hide it. jquery
我有一个预设的 7 个图像持有者div,我向 php 发送请求以从用户那里检索作品,所以假设如果有 3 个作品并且我添加到 3 个 img 持有者,我将有 4 个空图像div,我不知道如何检查以隐藏其余部分。
.HTML
<div class="portfolio_thumb">
<div class="portfolio_thumbImg_holder columns"><img src=""></div>
<div class="portfolio_thumbImg_holder active columns"><img src=""></div>
<div class="portfolio_thumbImg_holder columns"><img src=""></div>
<div class="portfolio_thumbImg_holder columns"><img src=""></div>
<div class="portfolio_thumbImg_holder columns"><img src=""></div>
<div class="portfolio_thumbImg_holder columns"><img src=""></div>
<div class="portfolio_thumbImg_holder columns"><img src=""></div>
</div>
Jquery
$('.portfolio_thumbImg_holder img').each(function(index, element) {
linksArray.push("http://localhost/testdatabase/cms/" + data[index].links);
$(element).attr("src", linksArray[index]);
titleArray.push(data[index].title);
$(element).attr("title", titleArray[index]);
descArray.push(data[index].desc);
$(element).attr("desc", descArray[index]);
alert("index:"+index + " " + linksArray[index] + " " + titleArray[index] + " " + descArray[index]);
});
如何改进 jquery 代码以隐藏所有其他没有加载图像的div
?
代码运行后,我
<div class="portfolio_thumbImg_holder columns"><img src=""></div>
成为
<div class="portfolio_thumbImg_holder columns"><img></div>
所以我不能使用
$(".portfolio_thumbImg_holder img[src='']").hide();
更新:
@jackDuong提供了一个我需要的代码
$(".portfolio_thumbImg_holder img:not([src])").parent().hide();
但是在我当前的代码上实现后,$.each 函数之后的所有代码都不会运行,知道为什么吗?
$.ajax({
type: "POST",
dataType: "json",
data: ({id: idArray[index]}), // pass the name of clicked holder into php to query
url: "CMS/PHP/retrieveAuthorWorks.php",
success: function(data) {
// reset all the arrays value to store new values upon new click
$('.portfolio_thumbImg_holder img').removeAttr("src");
linksArray=[];
titleArray=[];
descArray=[];
$(".full_image_desc .title").html(data[0].title);
$(".full_image_desc .info").html(data[0].desc);
$('.portfolio_thumbImg_holder img').each(function(index, element) {
linksArray.push("http://localhost/testdatabase/cms/" + data[index].links);
$(element).attr("src", linksArray[index]);
titleArray.push(data[index].title);
$(element).attr("title", titleArray[index]);
descArray.push(data[index].desc);
$(element).attr("desc", descArray[index]);
//$(".portfolio_thumbImg_holder img:not([src])").parent().hide();
});
// from here onwards code won't run
alert("hi");
$(".portfolio_thumbImg_holder img:not([src])").parent().hide();
}
});
我明白了...您在"portfolio_thumb"中添加了 7 棵榆树。
只需做:
$(".portfolio_thumbImg_holder img[src='']").hide();
如果要隐藏div(父级)只需做:
$(".portfolio_thumbImg_holder img[src='']").parent().hide();
试试这个例子:http://jsfiddle.net/duongtuanluc/2ktwa75e/
我看到你改变了问题。
如果标签 img 没有属性 src只需做:
$(".portfolio_thumbImg_holder img:not([src])").parent().hide();
试试这个例子http://jsfiddle.net/duongtuanluc/2ktwa75e/
你可以为此使用过滤器
$(".portfolio_thumbImg_holder").filter(function() {
var imgAttr = $(this).find("img").attr("src")
return imgAttr == typeof undefined || imgAttr == false
}).hide();
或者你可以循环访问每个div以检查它是否有图像,然后将其隐藏
$(".portfolio_thumbImg_holder").each(function() {
var imgAttr = $(this).find("img").attr("src")
if (imgAttr == typeof undefined || imgAttr == false) {
$(this).hide();
}
});
我自己试过,终于得到了。.
我先隐藏div
$(".portfolio_thumbImg_holder").hide();
检索完成后,假设 3 个项目 src 将添加到前 3 个div,然后我这样做
if($(this).has('src')){
$(".portfolio_thumbImg_holder:nth("+index+")").show();
alert("shown " + index);
}
else
{
}
最终代码
$.ajax({
type: "POST",
dataType: "json",
data: ({id: idArray[index]}), // pass the name of clicked holder into php to query
url: "CMS/PHP/retrieveAuthorWorks.php",
success: function(data) {
// reset all the arrays value to store new values upon new click
$('.portfolio_thumbImg_holder img').removeAttr("src");
linksArray=[];
titleArray=[];
descArray=[];
$(".full_image_desc .title").html(data[0].title);
$(".full_image_desc .info").html(data[0].desc);
$(".portfolio_thumbImg_holder").hide();
$('.portfolio_thumbImg_holder img').each(function(index, element) {
linksArray.push("http://localhost/testdatabase/cms/" + data[index].links);
$(element).attr("src", linksArray[index]);
titleArray.push(data[index].title);
$(element).attr("title", titleArray[index]);
descArray.push(data[index].desc);
$(element).attr("desc", descArray[index]);
alert("index:"+index + " " + linksArray[index] + " " + titleArray[index] + " " + descArray[index]);
if($(this).has('src')){
$(".portfolio_thumbImg_holder:nth("+index+")").show();
alert("shown " + index);
}
else
{
}
});
}
});
相关文章:
- Jquery自动完成:如果没有选择任何项目,则使tab键选择第一个项目
- 如果视图中没有Jquery脚本引用,Unobtrusive Javascript Validation是不起作用的
- 找到所有元素,如果没有类,则使用 jQuery 添加其他类
- 如果检查了输入,则将类应用于<身体>,没有jQuery
- JQuery动画打断了脚本,但如果没有它,脚本就可以工作
- 在更改时,如果没有使用jquery更改,也可以删除对select元素的关注
- jQuery倒计时隐藏天/小时/分钟(如果没有)
- 如果没有src,则使用jquery隐藏图像
- 如果没有jQuery,这段代码的并行性是什么
- 如果没有JavaScript中的jQuery,我如何读取.json文件
- jQuery如果没有类不触发功能
- 如果我想在没有jQuery的情况下操作HTML / CSS,我应该应用什么方法
- 如果没有数据,如何隐藏表格并使用jQuery或JavaScript显示表单
- jQuery检查项目是否在焦点中,如果没有,则对其采取行动
- 如果没有值,则使用 jQuery 删除
- 检查类是否由 img src 组成,如果没有,则隐藏它.jquery.
- 如果没有jQuery,javascript如何检查多个ajax调用是否已经完成,一旦完成,就执行一段代码
- 如果没有jquery,我需要确定鼠标是否在一个元素上,而不是确定它何时结束(以防它没有移动来触发onmouseover)
- 如果没有jQuery,则会从外部css文件或内部样式表中告知信息
- 如果没有jQuery, Webpack无法加载主干