最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove all inherited and computed styles from an element? - Stack Overflow

programmeradmin0浏览0评论

The page has the following CSS:

input[type=text]
{
    display: inline; padding: 7px; background-color: #f6f6f6; font-size: 12px; letter-spacing: 1px; border: 1px solid #aac7d1; 
    /* lots of other styles here... */
}

I have many text-input elements and the following:

<input type="text" id="txt1" />

And I try to apply different styles to this individual textbox (txt1) via jQuery:

$('#txt1').removeClass().removeAttr('style').css({
    'background-color': '#ff0000',
    //lots of other styles here...
});

But those styles that e from the style-sheet cannot be removed from the element this way. The css rule, input[type=text] is not a custom class so removeClass() does not work here, if I'm right.

What I want to do is; to pletely remove all styles ever applied to the element txt1. Is there a certain way to do this other than getting the list of all puted styles and setting them empty?

The page has the following CSS:

input[type=text]
{
    display: inline; padding: 7px; background-color: #f6f6f6; font-size: 12px; letter-spacing: 1px; border: 1px solid #aac7d1; 
    /* lots of other styles here... */
}

I have many text-input elements and the following:

<input type="text" id="txt1" />

And I try to apply different styles to this individual textbox (txt1) via jQuery:

$('#txt1').removeClass().removeAttr('style').css({
    'background-color': '#ff0000',
    //lots of other styles here...
});

But those styles that e from the style-sheet cannot be removed from the element this way. The css rule, input[type=text] is not a custom class so removeClass() does not work here, if I'm right.

What I want to do is; to pletely remove all styles ever applied to the element txt1. Is there a certain way to do this other than getting the list of all puted styles and setting them empty?

Share Improve this question edited Nov 21, 2011 at 1:18 Kit Ho 27.1k45 gold badges120 silver badges163 bronze badges asked Nov 20, 2011 at 0:41 Onur YıldırımOnur Yıldırım 33.7k14 gold badges85 silver badges98 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Is there a certain way to do this

There is no way to stop rules applying to an element if the selector matches.

You can either

  • set every property you want to have a specific value (with a suitably specific selector and !important
  • move the element to a different document (which could then be loaded with, for example, an iframe).
  • remove the rule (and thus cause it to not apply to any element)
  • change the selector on the ruleset (so it doesn't match the element any more, you have to be careful to make sure it still selects elements you care about)

other than getting the list of all puted styles and setting them empty?

That wouldn't work. Most properties won't accept a blank value, so the rule would be invalid and ignored (thus causing the previous rule to apply again).

As you can see at this link: http://www.w3/TR/CSS2/cascade.html#specificity, all selectors has specificity value. So, if you write such css rule: #txt1 {}, you can reset all unnecessary values.

发布评论

评论列表(0)

  1. 暂无评论