HTMLファイルからjquery onclickイベントを使用してstyle.cssファイルの属性プロパティを変更することは可能ですか?
現在下記を使用していますが、cssファイル自体を変更したいです。
$('#demo').click(function(){
$(.#demo').slideUp();
this.style.backgroundColor = 'red';
});
You cannot edit the file itself. However, you can override styles a number of ways.
style
element with the required rules and append to the document. (Example).css
to change the desired properties. (Example).addClass
and .removeClass
. (Example)For the type of behavior you describe, you'd use #2 or #3 (#3 is recommended because it's the easiest to maintain). Here's #2 in practice using your example: http://jsfiddle.net/HackedByChinese/HTNGs/1/
$('#demo').click(function() {
$(this).css('background-color', 'red');
});
css ファイル自体を編集することはできませんが、css プロパティを変更または追加することはできます。
これはあなたを助けるデモですhttp://jsfiddle.net/Simplybj/EFsvz/2/
次のように、複数のcssプロパティをhtmlに追加できます
$('#demo').click(function(){
$(this).css({'background-color':'red','color':'white');
});
または、スタイルシートで css Class が定義されている場合
.makeRed
{
background-color:red;
height:100px;
width:100px;
margin:5px;
}
.makeGreen{
background-color:green;
height:100px;
width:100px;
margin:5px;
}
そしてマークアップのような
<div id="me" class="makeRed"></div>
次に、divのクラスを次のように変更できます
$('#me').click(function(){
$(this).removeClass('makeRed').addClass('makeGreen');
});
you cannot edit css file, if you want to edit css property/attribute then you have to use like this
$(this).css("name","value");
so for you, it will be $(this).css("background-color","red");
instead of this.style.backgroundColor = 'red';
jquery だけを使用して CSS ファイルを編集することはできませんが、ファイルを開いたり保存したりできる php スクリプトを作成し、jquery を使用してそのスクリプトに css の変更を渡すことができます。
このリンクをチェックしてください: http://api.jquery.com/jQuery.post/
You cannot edit files on the server from the client-side.
This is simply not possible due to security reasons, sorry.
別の方法があり、変更されたプロパティと変更されていないプロパティを使用して個別の css クラスを定義します。次に、j query/java スクリプトを使用して、要素のクラスを簡単に変更できます。それがうまくいくことを願っています...