![](/img/trans.png)
[英]jQuery: Toggle class on jQuery element that has been added to the DOM via .wrap()?
[英]Apply styles regardless if the element has been added to the DOM with jQuery
問題:應用$(".price").hide();
適用於已經渲染的元素,但是,當我通過javascript加載新模板時,價格類將可見。
問題:有沒有一種在插入DOM后將樣式應用於類的所有將來實例的方法。
例:
$(".price").hide();
$('body').append('<div class="price">19.00</div>'); // this should be hidden.
我認為最好的解決方案是使用幫助器類:
CSS
.helper{
display: none;
}
jQuery的
$(".price").addClass('helper'); //for dyanmic addition to certain classes
$('body').append('<div class="price helper">19.00</div>'); //for adding afterward
而不是這樣做,而是使切換開關修改容器元素以添加或刪除類。
然后,創建一個如下所示的CSS規則:
.hidePrice .price{
display:none;
}
切換按鈕時,只需切換容器元素上的hidePrice類。
請參閱: http : //jsfiddle.net/bXChZ/
這對我有用:
使用jQuery:
$('<style id="priceVisibility">div.results .price {display: none; }</style>').appendTo('head');
沒有jQuery:
// Appending style element to the head with a overriding style.
var style = document.createElement('style');
style.type = 'text/css';
style.id = 'priceVisibility';
style.innerHTML = 'div.results .price {display: none; }';
document.getElementsByTagName('head')[0].appendChild(style);
您可以將一個類添加到<body>標記中,然后使用CSS隱藏.price元素。 例如
jQuery的:
$('body').addClass('hide-price');
CSS:
.price {display: block}
.hide-price .price {display: none}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.