[英]If div contains something change css of parent div
I am having html like this: 我有这样的html:
<div class="Proizvod" value="131" onclick="IdiNaProizvod(this)">
<div class="ProizvodInner">
<p class="KatBr">10-56 L </p>
<p class="Naziv">TN 35 SRAF 1000/1 </p>
</div>
</div>
<div class="Proizvod" value="131" onclick="IdiNaProizvod(this)">
<div class="ProizvodInner">
<p class="KatBr">10-88 L </p>
<p class="Naziv">TN 70 SRAF 1000/1 </p>
</div>
</div>
Now I have input
element with onclick
method that runs function which should do this: 现在,我有了带有
onclick
方法的input
元素,该元素运行应执行以下操作的功能:
Proizvod
contains input and if it is then change css of that div with class Proizvod
. Proizvod
类的任何HTML部分是否包含输入,然后通过Proizvod
类更改该div的CSS。 So let's say I typed in 88
and it goes 假设我输入了
88
<div class="Proizvod" value="131" onclick="IdiNaProizvod(this)"> /* There is no html part here - skip */
<div class="ProizvodInner"> /* There is no html part here - skip */
<p class="KatBr">10-56 L </p> /* There is html part here - it doesn't contain 88 - skip*/
<p class="Naziv">TN 35 SRAF 1000/1 </p> /* There is html part here - it doesn't contain 88 - skip*/
</div>
</div>
<div class="Proizvod" value="131" onclick="IdiNaProizvod(this)"> /* There is no html part here - skip */
<div class="ProizvodInner"> /* There is no html part here - skip */
<p class="KatBr">10-88 L </p> /* There is html part here - it does contain 88 - change this div with class Proizvod display to none */
<p class="Naziv">TN 70 SRAF 1000/1 </p>
</div>
</div>
I have tried something like this: 我已经尝试过这样的事情:
function Filter(element)
{
$t = $(element).val();
$("div:contains($t)").css( "display", "none" );
}
But nothing happens nor error appear. 但是什么也不会发生,也不会出现错误。
$t
is meaningless inside your selector string like that, it's just treated like aa literal string. 像这样,
$t
在您的选择器字符串内是没有意义的,它就像一个文字字符串一样对待。 There's no string interpolation in JS like in languages like PHP which would place its value into the final string. 像PHP这样的语言中,在JS中没有字符串插值法会将其值放入最终字符串中。
Try $("div:contains(" + $t + ")")
instead. 尝试使用
$("div:contains(" + $t + ")")
。
Also your code currently targets all divs, not just the ones with the class "Proizvod" as mentioned in the question. 另外,您的代码当前针对所有div,而不仅仅是问题中提到的“ Proizvod”类的div。
Here's a demo which fixes both of the above issues, and also resets the filter each time so that results hidden by the previous filter operation are visible again: 这是一个演示,它修复了上述两个问题,并且每次都重置了过滤器,从而使先前过滤器操作隐藏的结果再次可见:
$(function() { $("#FilterButton").click(function() { $t = $("#Filter").val(); $(".Proizvod").css("display", "block"); //reset previous filter $(".Proizvod:contains(" + $t + ")").css("display", "none"); //apply new filter }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="Proizvod" value="131" onclick="IdiNaProizvod(this)"> <div class="ProizvodInner"> <p class="KatBr">10-56 L </p> <p class="Naziv">TN 35 SRAF 1000/1 </p> </div> </div> <div class="Proizvod" value="131" onclick="IdiNaProizvod(this)"> <div class="ProizvodInner"> <p class="KatBr">10-88 L </p> <p class="Naziv">TN 70 SRAF 1000/1 </p> </div> </div> <input type="text" id="Filter" /><button type="button" id="FilterButton">Filter</button>
如果要使该语法起作用,则需要使用模板文字
$(`div:contains(${$t})`).css( "display", "none" );
The problem is with selection of the div 问题在于选择div
You need to apply $("div:contains(" + $t + ")")
for contains
check. 您需要为
contains
检查应用$("div:contains(" + $t + ")")
。
Please check below code, I removed some extra code, and apply class border so you can get more idea. 请检查下面的代码,我删除了一些额外的代码,并应用类边框,以便您了解更多信息。
function Filter() { $("body > div").removeClass('YesProizvod').addClass('Proizvod');//this is for reset all $t = $('#myFilter').val(); $("body > div:contains(" + $t + ")").removeClass('Proizvod').addClass('YesProizvod'); }
.Proizvod { border: solid 2px green; margin-bottom: 10px; } .YesProizvod { border: solid 2px red; margin-bottom: 10px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="myFilter" value="88" /> <button onclick="Filter()">Click here to filter</button> <br><br> <div class="Proizvod" value="131"> <div class="ProizvodInner"> <p class="KatBr">10-56 L </p> <p class="Naziv">TN 35 SRAF 1000/1 </p> </div> </div> <div class="Proizvod" value="131"> <div class="ProizvodInner"> <p class="KatBr">10-88 L </p> <p class="Naziv">TN 70 SRAF 1000/1 </p> </div> </div>
I hope it will be helpful. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.