3

インラインcssを作成する(またはJavaScriptを使用して追加する)と、cssホバーの値が失われることがわかりました。そのような振る舞いを変えることは可能ですか?

簡単な例


<div id="test" style="color: red">test</div>
<style>
#test:hover {
    color:blue;
}
</style>

この場合、ホバーは機能しません。

アップデート

!importantその後、JavaScriptを介してその属性を変更できなくなるため、使用できません。

また、スタイルを動的に生成するため、JavaScriptを介して特定のクラスを追加することはできません。

4

4 に答える 4

3

because inline css overrides your css styles in the file if you had

color: blue !important

it would work but not recommended, you can always use jquery to remove the inline style tag though haha

Update:

remove the style tag using jquery or when using javascript... add !important so the inline css would have important

于 2012-05-17T20:12:05.880 に答える
3

I can't use !important, because after it i will not be able to change that atribute via javascript.

You can still use !important, just add a class with JavaScript and remove it anytime you want.

demo

于 2012-05-17T20:12:15.463 に答える
3

最も簡単な方法は次のとおりです

$(selector).hover(function() {
                $st = $(this).attr("style");
                $(this).attr("style","");
            },function() {
                $(this).attr("style",$st);
            });
于 2012-05-17T20:30:08.970 に答える
0

CSS では、style属性はすべてのstyle要素またはstylesheets. 属性を変更できない場合はstyle、 を使用する必要があります!importantstyle属性を完全に削除して、すべてを要素に配置できる場合style。またはさらに良いことに、別stylesheetの方法で問題が解決されます。

PS要素typeには属性が必要です。styleこれで問題は解決しませんが、次のように変更<style>する必要があります<style type="text/css">

于 2012-05-17T20:15:07.193 に答える