2012-01-03 158 views
2

新年快乐。通过计算.attr()值来设置div高度

我试图通过计算一系列元素的.attr()值来设置div的高度。

在HTML

<div id="buttons"> 
    <div id="add-x">Add X</div> 
    <div id="most-x">Most X</div> 
    <div id="least-x">Least X</div> 
    <div id="add-class">Add Classes</div> 
</div> 

<div id="calculate"> 
    <div class="add-x a" x="10">Sample Text</div> 
    <div class="add-x b" x="20">Sample Text</div> 
    <div class="add-x c" x="30">Sample Text</div> 
    <div class="add-x d" x="40">Sample Text</div> 
    <div class="add-x e" x="50">Sample Text</div> 
</div> 

<div id="height-box">Height Box</div> 

JS的

var total = null; 

$('#add-x').on('click', function(event) { 
    $('#calculate .add-x').each(function() { 
     total += parseFloat($(this).attr('x')); 
    }); 
alert(total) 
}); 

var most = null; 

$('#most-x').on('click', function(event) { 
    $('#calculate .add-x').each(function() { 
    var value = parseFloat($(this).attr('x')); 
    most = (value > most) ? value : most;  
    });  
alert(most) 
}); 

var least = null; 

$('#least-x').on('click', function(event) { 
    $('#calculate .add-x').each(function() { 
    var value = parseFloat($(this).attr('x')); 
    least = (value < least) ? value : least; 
    }); 
alert(least) 
}); 

此外,我想求信类设定的值(类= 10px的,类B = 20像素,类别c = 30像素,类d = 40px和类e = 50px),并按照与.attr()相同的方式将它们计算为总,最大和最小值。这里的目的是不要在html中使用属性。

我的问题是:

1)。如何以与获得最大价值相同的方式获得.attr('x')的最小值?

2.)如何将它存储在var中,以便我可以使用.on()将其应用于元素的高度? 3.如何为类指定一个值,然后使用.on()计算它,并确定一个元素的高度?

下面是我在哪里:http://jsfiddle.net/ifthatdoesntdoit/chQdK/58/

感谢

+0

仅供参考,如果你有一个不相关的多个问题(即使是在同一个代码),请一定要问多个问题,而不是让他们都在同一个。不同的专家可以关注个人问题;你很容易正确地标题;如果对于每个不同的问题都有多个正确的答案,那么您如何才能让他们中的一个成为“正确的”答案? – iND 2012-01-03 18:37:58

+0

会做什么,国内。感谢您的高举。我会牢记这一点。感谢您的回应。我仍然会最终使用它,但没有得到它。只要我能够再次工作,我会接受一个答案。谢谢你的耐心。 – ifthatdoesntdoit 2012-04-02 07:13:33

回答

0

范围界定总。改变这种

var total = 0; 

$('#add-x').on('click', function(event) { 
    total = 0; 
    $('#calculate .add-x').each(function() { 
     total += parseFloat($(this).attr('x')); 
    }); 
    alert(total); 
}); 

和这台最高度:

var most = 0; 

$('#most-x').on('click', function(event) { 
    most = 0; 
    $('#calculate .add-x').each(function() { 
     var value = parseFloat($(this).attr('x')); 
     most = (value > most) ? value : most; 

    }); 
    alert(most); 

    $('#height-box').height(most); 

}); 

和至少:

$('#least-x').on('click', function(event) { 
    least = 1000000; 
    $('#calculate .add-x').each(function() { 
     var value = parseFloat($(this).attr('x')); 
     least = (value < least) ? value : least; 

    }); 

    alert(least) 
    $('#height-box').height(least); 

}); 

你想真的应该在一个单独的问题被问的其他事情。要回答的东西太多,所以很难集中答案。 。 。 。

+0

iND - 感谢您的帮助。但是,粘贴代码后,点击总数,最多和最少按钮时不会发生任何事情。我的经验有限,请耐心等待。这里是最新的jsfiddle:http://jsfiddle.net/ifthatdoesntdoit/chQdK/68/你可能会更新这个例子并让它工作吗? - 谢谢 – ifthatdoesntdoit 2012-01-03 17:41:25

+0

问题是拼写错误 - 抱歉!固定。 – iND 2012-01-03 18:33:52

0

下面是回答所有问题的代码。请阅读代码中的注释。希望有所帮助。

<div id="calculate"> 
<div class="10px">Sample Text</div> 
<div class="20px">Sample Text</div> 
<div class="30px">Sample Text</div> 
<div class="40px">Sample Text</div> 
<div class="50px">Sample Text</div> 
</div> 


var total = null, 
    $calculateDiv = $('#calculate div'), 
    $heightBox = $('#height-box'), 
    once = true; 

$('#add-x').on('click', function(event) { 
    if(once) { //Calculate total only once. 
     $calculateDiv.each(function() { 
      total += parseFloat($(this).attr('class')); 
      once = false; 
     }); 
    } 
    $heightBox.height(total); // Set the height of green box after calculating total 
}); 



var most = null; 

$('#most-x').on('click', function(event) { 
    $calculateDiv.each(function() { 
     var value = parseFloat($(this).attr('class')); 
     most = (value > most) ? value : most; 

    }); 

$heightBox.height(most); // Set the height of green box after calculating 'most' 

}); 


var least = null; 


$('#least-x').on('click', function(event) { 
    least = parseFloat($calculateDiv.eq(0).attr('class')); // Set it to the first value before comparing. 
    $calculateDiv.each(function() { 
     var value = parseFloat($(this).attr('class')); 
     least = (value < least) ? value : least; 

    }); 

    $heightBox.height(least); // Set the height of green box after calculating 'least' 

}); 
+0

Sandeep-感谢您检查我的代码。不幸的是,我一直无法得到它的工作。我与js的经验有限,所以我感谢您的耐心。这里是最新的jsfiddle:http://jsfiddle.net/ifthatdoesntdoit/eGtXp/3/你介意试着让它工作。在警报()我只得到'NaN'和null。 - 谢谢 – ifthatdoesntdoit 2012-01-03 17:45:54

+0

那里,我修好了。请注意,'#calculate' div中的DIV不能有其他类名。如果你想避免非标准属性,那么使用'rel'属性而不是类怎么样? – Sandeep 2012-01-03 20:19:39