2012-06-09 50 views
2

我有一个函数,它使用缓存元素(el)更改字体大小2 divs。当函数执行时,我需要确定字体不是太小或太大,取决于它是哪个div(tagCommontagLatin)。那么如何在函数中确定哪个元素通过el传递给它?jQuery缓存元素

我想我可能已经在考虑这个或者做错了,它有点像我黑客它在......并且通常当它觉得有什么不对劲的时候。

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 
} 

谢谢你的时间。

托德

回答

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对象元件。返回值布尔值true/false。

3

使用jQuery is()方法

检查当前匹配组针对一个选择器,元件, 或jQuery对象元素,并且如果这些元素中的至少一个 匹配给定返回true参数。

if(el.is('#tagCommon')) 
    { 
     // your code here 
    }