0

私が見たすべての例では、「getElementById」を使用して単一の要素を取得し、そのスタイルをその単一の要素に変更しています。

ページ上の一致するすべての要素をスタイルで変更する必要がある状況にあります。フォントサイズ、高さ、幅を変更する必要があります。jQuery は必須ですか、それともオプションですか? 私が質問する理由は、このサイトでは jQuery を使用していないためです。この 1 つのことを達成するためだけにライブラリ全体をダウンロードしたくありません。

更新 例として、ページに次のスタイルの要素がいくつかあるとします。

.outerContact
{
    border: 0px;
    margin: 7px;    
    float: left;
   padding: 0px; 
 background: url(/TLSADMIN/UserControls/contactsgrid/trans-shadow.png) no-repeat bottom right; /* Most major browsers other than IE supports transparent shadow. Newer release of IE should be able to support that. */        
}
.contactLarge{
    height: 163px;
    width: 250px;
    border: 1px solid #ccc; 
    border-top: 1px solid #ddd;
    font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
    font-size:small;
    margin: -5px 5px 5px -5px; /* Offset the image by certain pixels to reveal the shadow, as the shadows are 6 pixels wide, offset it by that amount to get a perfect shadow */

  /* Most major browsers other than IE supports transparent shadow. Newer release of IE should be able to support that. */     
     background-image: url('/TLSADMIN/UserControls/contactsgrid/OutlookContactGradient.png') ;
     background-repeat:repeat-x;
      background-position: bottom;
padding: 0px; /* Increasing this gives a white border around the image */ 
background-color: #f2f6f9; /* Background color of the border created by the padding */
border: 1px solid #cecece; /* A 1 pixel greyish border is applied to the white border created by the padding */ 
display: block; /* IE won't do well without this */ 
position: relative; /* Make the shadow's position relative to its image */ 

}

次に、スライダー バーに従って上の要素のサイズを比例的に変更する JavaScript 関数があるとします。私が使用しているスライダー バーは、サード パーティから入手できます: http://aspnetajax.componentart.com/control-specific/slider/features/custom_Scrollbar/WebForm1.aspx

そのスライダーは、ズームイン/ズームアウトする量を決定するために使用する数値を渡します。

function ResizeWindowAccordingToScrollBar(PercentChange)
{
 //Locate elements
 //Resize fonts, borders, all styles to zoom in/ zoom out.

}

「PercentChange」値をどのように処理することをお勧めしますか? switch ステートメントを使用して、一致する各要素の CSS スタイルを入れ替えることはできますが、他のオプションほどスムーズではない可能性があります。

更新2

また、誰かが私のコードを見たい場合は、自己完結型のサンプルがここにあります: http://www.perfmon.com/download/StackOverflow_ContactsGrid.zip

ComponentArt コントロールをダウンロードする場合は、スクロールバー コードのコメントを自由に外してください。

私の目標は、Outlook 2007 で利用可能なズーム機能を直接エミュレートすることです。

代替テキスト

4

3 に答える 3

3

getelementbyclassname をグーグルで検索すると、役立つ場合があります。getelementbyid と同様の考え方ですが、クラス名でそれらを取得し、必要に応じてスタイルを変更できます。

于 2010-08-12T15:50:51.127 に答える
2

では、このような HTML があるとしましょう...

<div>
    <h2>Example</h2>
    <p>This is an example</p>
    <p>
        Some of this content can be re-sized.
    </p>
    <img src="lenna.jpg" class="iconic" alt="a petty lady" />
</div>

これらの要素の一部をズーム可能/サイズ変更可能にする必要があります。まず、要素識別子を使用してこれらの要素を識別する必要があります。id属性または属性のいずれかclass

2 つの違いは、id属性が一意でなければならないことです。いくつかの項目をズーム可能として識別する必要があるため、class代わりに属性を使用します。のような適切で自明な値を使用しますzoomable

<div>
    <h2>Example</h2>
    <p>This is an example</p>
<div>
    <h2>Example</h2>
    <p>This is an example</p>
    <p class="zoomable">
        Some of this content can be re-sized.
    </p>
    <img src="lenna.jpg" class="zoomable iconic" alt="a petty lady" />
</div>
</div>

<img>要素にはすでに属性がありましたが、 w3cの推奨classに従って、その要素を複数のクラスに割り当てることができます。

この属性は、クラス名またはクラス名のセットを要素に割り当てます。任意の数の要素に同じクラス名を割り当てることができます。複数のクラス名は空白文字で区切る必要があります。

これで、マークアップがわかりました。次に JavaScript です。

それが完了したら、document.getElementsByClassName(...)関数を使用してこれらすべてのzoomable要素への参照を取得できます。

var zoomables = document.getElementByClassName('zoomable');
for(var i=0; i<zoomables.length; i++) {
    zoomables[i].style.height = '100px';
    zoomables[i].style.width = '100px';
    zoomables[i].style.fontSize = '20px';
}

...しかし、Internet Explorer ではサポートされていないため、オブジェクト検出を使用して、代わりに定義する必要があるかどうかを判断する必要があります。

if(!document.getElementsByClassName) {
    document.getElementsByClassName = function(className) { // this is the simplest, plainest, lowest-invested-time-and-effort
                                                            // version of getElementsByClassName  one can write...
        // you should probably sanitize that className input parameter a bit...
        var allElements = document.getElementsByTagName('*');
        for(var i=0; i<allElements.length; i++) {
            if(/\bclassName\b/.test(allElements[i].className)) {
                allElements[i].style.border = "2px solid red";
            }
        }
    }
}

これで問題が完全に解決するわけではありませんが、正しい道に進むはずです。

于 2010-08-12T18:32:12.407 に答える
0

CSSファイルに必要なクラスを作成し、jQueryで変更することをお勧めします。

.class1{background-color:#ff0000;}
.class2{background-color:#0000ff;}

そしてjQueryでは:

$("#element").click(function(){
    $(".class1").removeClass("class1").addClass("class2");
});

このように、必要な結果を得るために「奇妙な」複雑なことをする必要はありません。

于 2010-08-12T15:55:32.470 に答える