从数组中获取数字并将此数字设置为其他函数的函数

Function which get number from array and set this number to other function

本文关键字:函数 数字 设置 其他 数组 获取      更新时间:2023-09-26

我对一些jquery代码有问题:)

情况如下:

  1. 我有一个包含数字的数组var arr

  2. 我有一个函数get_number(),它从大堆

  3. 我有一个参数为inner_height(number)的函数,它可以获得元素的最大高度,并将该高度设置为其他元素

但有些东西崩溃了…:(

这是一个jsfiddle


HTML结构

<div id="slide-1">
    <div class="inner-li">
        <p>Lorem Ipsum...</p>
    </div>
</div>
<div id="slide-2">
    <div class="inner-li">
        <p>Lorem Ipsum...</p>
    </div>
</div>
<div id="slide-3">
    <div class="inner-li">
        <p>Lorem Ipsum...</p>
    </div>
</div>


带数字的数组//将数字保存在阵列中

var arr = [];
$("div[id^='slide-']").each(function() {
    arr.push(parseInt( $(this).attr('id').split('-')'[1] ));
}); 


从阵列中获取每个数字

// Get each number from array
function get_number() {
    $.each(arr, function(i, v) {
        return arr[i];
    });
}


获取内部li元素的最大高度

// Get max-height of inner-li and set to the others with the same class
function inner_li_height(number) {
    var heights = $("div[id^='slide-" + number + "'] .inner-li").map(function () {
        return $(this).height();
    }).get(),
    maxHeight = Math.max.apply(null, heights);
    console.log(maxHeight);
    $("div[id^='slide-" + number + "'] .inner-li").height(maxHeight);
}


这里调用函数:

// Call function inner_li_height
inner_li_height(get_number);


如果有人能帮我,我会很高兴:)

试试这个在多个DOM对象上工作的函数:

// Get max-height of inner-li and set to the others with the same class
function inner_li_height(attr) {
   $(attr).each(function(i,v) {
    var heights = $(attr).map(function () {
        return $(this).height();
    }).get();
    maxHeight = Math.max.apply(null, heights);
    console.log(maxHeight);
    $(attr).height(maxHeight);
   });
}
inner_li_height('div[id^="slide-"] .inner-li');

jsfiddle:http://jsfiddle.net/hr993Lsj/1/