Javascript 如何查找元素当前的显示方式?
我必须存储元素的Javascript 如何查找元素当前的显示方式?,javascript,jquery,Javascript,Jquery,我必须存储元素的display属性的值(none,block,inline…),以便以后还原它element.style.display返回空字符串。如何获得计算值 该解决方案可以使用jQuery或其他库 $("#myElemId").css("display"); 这将为您提供可以存储的显示属性,在这种情况下,正如他们所说:显示属性是自动存储的 var myDisplay = $('#YourControl').css('display'); 如果要在元素中存储其他数据 $("#eleme
display
属性的值(none
,block
,inline
…),以便以后还原它<如果从css样式表设置了display
属性,则code>element.style.display返回空字符串。如何获得计算值
该解决方案可以使用jQuery或其他库
$("#myElemId").css("display");
这将为您提供可以存储的显示属性,在这种情况下,正如他们所说:显示属性是自动存储的
var myDisplay = $('#YourControl').css('display');
如果要在元素中存储其他数据
$("#element").data('foo', 52);
要还原存储在此元素中的数据
var mystoredData = $("#element").data('foo');
如果没有jQuery,可以使用
getComputedStyle
检测显示属性值:
window.getComputedStyle(YOUR_DOM_ELEMENT).cssText.split('display: ')[1].split(';')[0]
如果您有jQuery,那么使用其他人所说的
.css('display')
。您可以将display属性存储到每个元素的.data()
然后像这样检索它:
$('#undo').on('click',function(){
$('.box').each(function(){
$(this).css({display: $(this).data('storedDisplayProp') }); // read stored data and reset
});
});
你能发布你的html代码吗?
$('.box').on('click',function(){
displayProp = $(this).css('display'); // read the .css display property
$(this).data('storedDisplayProp', displayProp); // store it into element data
$(this).css({display:'none'}); // modify the .css display
});
$('#undo').on('click',function(){
$('.box').each(function(){
$(this).css({display: $(this).data('storedDisplayProp') }); // read stored data and reset
});
});