简体   繁体   English

如何使用JQuery仅删除Style attr的位置(顶部和左侧)?

[英]How to remove only position(top and left) of Style attr using JQuery?

I am dynamically setting the position attr of the element only when the view is out of viewport of the window, in other case default value is set from the css file , 我只在视图超出窗口的视口时动态设置元素的位置attr,在其他情况下,默认值是从css文件设置的,

.css( { "left": (left + 20) + "px", "top": (top+10) + "px" } );

Once the dynamic position is set, I want to remove the position attr alone. 一旦设置了动态位置,我想单独删除位置attr。

I can remove style attribute it will also my display property of style which is required. 我可以删除样式属性,它也将是我需要的样式的显示属性。

Is there a way to to remove the position attr alone? 有没有办法单独删除位置attr?

Perhaps, your best Bet would be to simply put them to their default value. 也许,你最好的赌注就是简单地将它们设置为默认值。 Top and left have the default value "auto". 顶部和左侧的默认值为“auto”。 So: 所以:

jQuery(selector).css({
   'top': 'auto',
   'left': 'auto'
})

$(el).css('top', ''); will remove the inline style declaration, reverting back to either what is specified in your stylesheet or the default. 将删除内联样式声明,恢复为样式表中指定的内容或默认值。

I'm not sure if you're asking for removing a position HTML attribute, a position style, or top and left styles. 我不知道,如果你问移除position HTML属性, position风格,或topleft的风格。 In any case, you can remove any attribute with $.removeAttr() , and you can remove (dynamically assigned) styles with $.css({ styleName: '' }) ; 在任何情况下,您都可以使用$.removeAttr()删除任何属性,并且可以使用$.css({ styleName: '' })删除(动态分配)样式;

Edit: it seems you want $.css({ position: '' }) , or more likely $.css({ position: 'static' }) , as that is the default value. 编辑:似乎你想要$.css({ position: '' }) ,或者更可能是$.css({ position: 'static' }) ,因为这是默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM