2

我有一个使用缓存元素(el)来更改2 divs的字体大小的函数。当函数执行时,我需要根据它是哪个 div(tagCommontagLatin)确保字体不会太小或太大。那么如何在函数中确定通过哪个元素传递给它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 对象检查当前匹配的元素集,如果这些元素中至少有一个与给定参数匹配,则返回 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 并与提供的匹配

.is()与选择器、元素或 jQuery 对象匹配的元素集。返回值布尔值真/假。

于 2012-06-09T14:26:19.817 回答