2

キャッシュされた要素 (el) を使用して2 divのフォント サイズを変更する関数があります。関数が実行されるとき、フォントが小さすぎたり大きすぎたりしないようにする必要があります (tagCommonまたはtagLatin)。では、関数でどの要素が渡されたかをどのように判断できelますか?

私はこれを考えすぎているか、間違っているのではないかと思います。ハッキングしているように感じます...そして通常、何かが間違っていると感じたとき.

var cb               = $('#tagCommon');
var lb               = $('#tagLatin'); 
changeFontSize(cb,1,'up');

function changeFontSize(el,amount,UporDown){
   var size = parseFloat($(el).css("font-size").replace(/px/, ""));
   // do some stuff here

   // ????????
   if(el == $('#tagCommon')) //check font size and alert if too small
   if(el == $('#tagLatin')) //check font size and alert if too small
}

お時間をいただきありがとうございます。

トッド

4

2 に答える 2

3

jQueryの is()メソッドを使用する

現在一致している要素のセットをセレクター、要素、または jQuery オブジェクトに対してチェックし、これらの要素の少なくとも 1 つが指定された引数と一致する場合は true を返します。

if(el.is('#tagCommon'))
    {
      //  your code here
    }
于 2012-06-09T14:25:20.457 に答える
1
function changeFontSize(el,amount,UporDown){
   var size = parseFloat($(el).css("font-size").replace(/px/, "")),
       id = el.attr('id'); // or el[0].id

   if(id == 'tagCommon') //check font size and alert if too small
   if(id == 'tagLatin') //check font size and alert if too small

   // OR
   if(id == 'tagCommon')
   if(id == 'tagLatin')

   // OR
   if(el.is('#tagCommon'))
   if(el.is('#tagLatin'))
}

.attr('id')ID を取得し、指定された ID と一致させます

.is()セレクター、要素、または jQuery オブジェクトに対して要素のセットを一致させます。戻り値 boolean true/false。

于 2012-06-09T14:26:19.817 に答える