我在堆栈上看到了很多东西,以获得最高的div,只有非为他们工作。 我需要获得div preview_txt的高度。
<div class="headline" data-rating="0.482005543693" onclick="javascript:showArticle(1076);" style="display: none; "> <div class="headline_txt"> <h1>#3726: Meryl Streep schittert in The Iron Lady- alle recensies op een rijtje</h1> </div> <div class="preview_txt"> <p> De eerste foto's van actrice Meryl Streep als de Britse voormalig premier Margaret Thatcher <a href="http://www.guardian.co.uk/film/2011/feb/08/meryl-streep-margaret-thatcher-iron-lady#"> leidden </a> vorig jaar al tot een stortvloed aan publiciteit. Nu is <a href="http://www.imdb.com/title/tt1007029/"> <em>The Iron Lady</em> </a> er dan ook echt. Vanaf vandaag draait de film in de Nederlandse bioscopen. Lees hier alle recensies. <!--more--> </p> </div> </div>从我看到的东西我觉得我最喜欢这个:
var maxHeadLineHeight = Math.max.apply(Math, $("#headline").map( function(){ return $(this).height(); } )); console.log("maxHeadLineHeight: "+maxHeadLineHeight);但这给了我:
Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type
我也尝试过:
"#headline .preview_txt"在它的位置,设置每个标题div的高度将是下一个,但可能不会那么不同(通过获取对象)。
I saw a lot of things on stack overflor to get the highest div, only non of them where working for me. I need to get the height of the div preview_txt.
<div class="headline" data-rating="0.482005543693" onclick="javascript:showArticle(1076);" style="display: none; "> <div class="headline_txt"> <h1>#3726: Meryl Streep schittert in The Iron Lady- alle recensies op een rijtje</h1> </div> <div class="preview_txt"> <p> De eerste foto's van actrice Meryl Streep als de Britse voormalig premier Margaret Thatcher <a href="http://www.guardian.co.uk/film/2011/feb/08/meryl-streep-margaret-thatcher-iron-lady#"> leidden </a> vorig jaar al tot een stortvloed aan publiciteit. Nu is <a href="http://www.imdb.com/title/tt1007029/"> <em>The Iron Lady</em> </a> er dan ook echt. Vanaf vandaag draait de film in de Nederlandse bioscopen. Lees hier alle recensies. <!--more--> </p> </div> </div>From the things i saw arround i think i liked this one the most:
var maxHeadLineHeight = Math.max.apply(Math, $("#headline").map( function(){ return $(this).height(); } )); console.log("maxHeadLineHeight: "+maxHeadLineHeight);But that gives me:
Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type
I also tried it like:
"#headline .preview_txt"while where at it, setting the height for each headline div will be next but that probably won't be that different (by getting the objects).
最满意答案
代码有两个问题:
你的选择器错了。
$("#headline")将找到headline为id的元素,而不是headline 类 。
试试这个:
$(".headline").map(...你忘了在.map()之后加入.get() .map()
我不确定为什么这很重要,但是类似于你发布的代码的例子都说你需要打电话:
$('<someSelectorHere>').map(function () { /* some mapping code here ... */ }) .get(); // <-- The part you missed - won't work without it看这个例子:
https://stackoverflow.com/a/5052710/232593编辑:
根据评论和jQuery文档,似乎调用.get()方法很重要,因为jQuery返回的对象不是真正的数组 - 它们只是伪装成数组。 虽然有些浏览器可以使用它(看似Firefox),但其他浏览器无法处理它。 因此,您需要添加.get()以转换为真实数组并支持这些浏览器。
演示您提供的代码的工作版本:
http://jsfiddle.net/zvu2n/该演示中的Javascript:
var headlineHeights = $(".headline").map(function() { return $(this).height(); }).get(); var maxHeadLineHeight = Math.max.apply(Math, headlineHeights); alert("maxHeadLineHeight: " + maxHeadLineHeight);There are two problems with the code:
1. Your selector is wrong.
$("#headline") will find an element with an id of headline, not a class of headline.
Try this instead:
$(".headline").map(...2. You forgot to include .get() after .map()
I'm not sure why this is important, but examples similar to the code you've posted all say you need to call:
$('<someSelectorHere>').map(function () { /* some mapping code here ... */ }) .get(); // <-- The part you missed - won't work without itSee this example:
https://stackoverflow.com/a/5052710/232593Edit:
According to comments and the jQuery docs, it seems that calling the .get() method is important because the objects returned by jQuery aren't truly arrays - they just masquerade as arrays. While some browsers are okay with this (seemingly Firefox), other browsers can't deal with it. So you need to add the .get() to convert to a real array and support those browsers.
Demo of a working version of the code you provided:
http://jsfiddle.net/zvu2n/The Javascript from that demo:
var headlineHeights = $(".headline").map(function() { return $(this).height(); }).get(); var maxHeadLineHeight = Math.max.apply(Math, headlineHeights); alert("maxHeadLineHeight: " + maxHeadLineHeight);更多推荐
发布评论