2

HTMLファイルからjquery onclickイベントを使用してstyle.cssファイルの属性プロパティを変更することは可能ですか?

現在下記を使用していますが、cssファイル自体を変更したいです。

$('#demo').click(function(){
    $(.#demo').slideUp();
     this.style.backgroundColor = 'red';
});
4

6 に答える 6

10

You cannot edit the file itself. However, you can override styles a number of ways.

  1. Create a new style element with the required rules and append to the document. (Example)
  2. Select elements and apply .css to change the desired properties. (Example)
  3. Define CSS selectors (possibly in concert with #1 above), and add or remove them from items using .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');
});​
于 2012-06-12T04:23:25.463 に答える
2

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');
});  
于 2012-06-12T04:45:38.217 に答える
1

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';

于 2012-06-12T04:23:21.020 に答える
1

jquery だけを使用して CSS ファイルを編集することはできませんが、ファイルを開いたり保存したりできる php スクリプトを作成し、jquery を使用してそのスクリプトに css の変更を渡すことができます。

このリンクをチェックしてください: http://api.jquery.com/jQuery.post/

于 2012-06-12T04:31:55.297 に答える
0

You cannot edit files on the server from the client-side.

This is simply not possible due to security reasons, sorry.

于 2012-06-12T04:23:01.710 に答える
0

別の方法があり、変更されたプロパティと変更されていないプロパティを使用して個別の css クラスを定義します。次に、j query/java スクリプトを使用して、要素のクラスを簡単に変更できます。それがうまくいくことを願っています...

于 2012-06-12T05:05:33.687 に答える