使用jquery只删除位置相关的内联样式

remove only position relative inline style using jquery

本文关键字:样式 jquery 删除 位置 使用      更新时间:2023-09-26

我有内联样式的

<div class="handle" id="parent4" value="3" size="large" style="position: relative; top: 0px; left: 0px; z-index: 0; cursor: pointer;"></div>

我想使用jquery 删除上面代码中的位置相对样式

<div class="handle" id="parent4" value="3" size="large" style= top: 0px; left: 0px; z-index: 0; cursor: pointer;"></div>

尝试用static、覆盖位置

$('#parent4').css('position','static');

因为CCD_ 2是任何div元素的默认位置。

要删除样式,可以使用.css(stylename,'')语法。所以在你的情况下使用这个:

$('#parent4').css('position','');

将位置设置为static,这是默认值。

以下是删除的代码

(function($)
    {
        $.fn.removeStyle = function(style)
        {
            var search = new RegExp(style + '[^;]+;?', 'g');
            return this.each(function()
            {
                $(this).attr('style', function(i, style)
                {
                    return style.replace(search, '');
                });
            });
        };
    }(jQuery));

使用这个迷你插件,你可以在JS文件中编写

$('#element').removeStyle('position');