问题:将$(".price").hide();
作品应用于已经渲染的元素,但是,当我通过 javascript 加载新模板时,价格类将可见。
问题:有没有办法在插入 DOM 时将样式应用于类的所有未来实例。
例子:
$(".price").hide();
$('body').append('<div class="price">19.00</div>'); // this should be hidden.
问题:将$(".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}