Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 元素属性上的jquery操作_Javascript_Jquery_Html_Dom - Fatal编程技术网

Javascript 元素属性上的jquery操作

Javascript 元素属性上的jquery操作,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,所以问题是: 我正在用json生成一个html块,最终的html如下所示: <section class="imagesec" img-width="400"> <button>click me</button> <img src="xyx.jpg"> <!-- image actual width = 400px --> <hr> </section> <section class

所以问题是: 我正在用json生成一个html块,最终的html如下所示:

<section class="imagesec" img-width="400">
    <button>click me</button>
    <img src="xyx.jpg"> <!-- image actual width = 400px -->
    <hr>
</section>
<section class="imagesec" img-width="300">
    <button>click me</button>
    <img src="abc.jpg"> <!-- image actual width = 300px -->
    <hr>
</section>

and so it goes on ....
...
...
然后显示所有包含宽度大于350像素的图像的部分。 这样做的一种方法是,获取所有的section元素,然后对每个元素进行循环,获取属性img width的值,并将其与350进行比较,如果它大于350,则显示当前html对象,否则将其隐藏

但我想知道这样的东西在jquery中是否可行

$("section.imagesec").hide()
$("section.imagesec").having(attr(img-width) > 350).show()
简而言之:我只想隐藏所有包含图像的部分 宽度小于350px

编辑:img width=400非img width=400px尝试使用。使用其回调进行筛选以完成任务

$('section.imagesec[img-width]').filter(function(){
  return (parseInt($(this).attr('img-width'),10) < 350);
}).hide();
尝试使用.filter及其回调来完成任务

$('section.imagesec[img-width]').filter(function(){
  return (parseInt($(this).attr('img-width'),10) < 350);
}).hide();
你可以使用过滤器

你可以使用过滤器


修复html5语法并使其更加正确。您应该使用基于html的数据元素来存储值。下面是新的html和来自其他答案的jQuery,我对此不予认可


修复html5语法并使其更加正确。您应该使用基于html的数据元素来存储值。下面是新的html和来自其他答案的jQuery,我对此不予认可


除了img宽度处的值类似于px@ಠ_ಠ 我没看到。。谢谢@ಠ_ಠ 不要紧-parseInt忽略字符末尾的非数字strings@Pointy我发表评论时,parseInt调用不在那里;哇@RajaprabhuAravindasamy这是一个非常快速的编辑:除了img宽度的值类似于px@ಠ_ಠ 我没看到。。谢谢@ಠ_ಠ 不要紧-parseInt忽略字符末尾的非数字strings@Pointy我发表评论时,parseInt调用不在那里;哇@RajaprabhuAravindasamy这真是一个快速的编辑:你可能应该在节中使用其他属性,然后使用img width。@Dejan.S如果我使用了prop或property,你能告诉我解决方案吗?@VarunVerma在下面发布了一个答案,以向你展示更正确的html5语法。你可能应该在节中使用其他属性,然后使用img width。@Dejan.S如果我使用了prop或property,你能告诉我解决方案吗?@VarunVerma在下面发布了一个答案,向你展示更正确的html5语法。
$("section.imagesec").hide().filter(function(){
   return parseInt($(this).attr('img-width'),10) > 350 ;
}).show();
<section class="imagesec" data-img-width="400">
    <button>click me</button>
    <img src="xyx.jpg"> <!-- image actual width = 400px -->
    <hr>
</section>


$('section.imagesec[data-img-width]').filter(function(){
  return (parseInt($(this).attr('data-img-width'),10) < 350);
}).show();