[英]How to hide item in html with jquery using variable
我正在嘗試使用這個簡單的腳本按ID隱藏某些元素,我將ID放在變量中,但是它不起作用..這是代碼:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
<script>
function filter(id_get) {
$(".product").each(function(index) {
var f = this.id;
if (!f.includes(id_get)) {
var hash = "#";
var cmp = hash.concat(f);
$(cmp).attr("display", "none");
}
});
}
</script>
如果我執行console.log(cmp)
它將顯示要刪除的正確產品ID,但不會隱藏div。
我也嘗試過$(cmp).hide
您正在嘗試直接更改屬性。 display
不是屬性。 style
是一個屬性。 您可以更改style
屬性的display
屬性。 更改$(cmp).attr("display","none");
至:
$(cmp).css("display", "none");
或者,您可以只使用內置的jQuery hide
功能。 更改$(cmp).attr("display","none");
至:
$(cmp).hide();
在上下文中:
function filter(id_get){
$( ".product" ).each(function( index ) {
var f = this.id;
if(!f.includes(id_get)){
var hash = "#";
var cmp = hash.concat(f);
$(cmp).hide();
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.