如何找到内部元素的宽度(How to find width of inner elements)
我找不到任何其他问题提出同样的问题,虽然这可能是我的搜索语句的问题。
我想弄清楚如何找到包含在一个固定宽度的容器div内的所有元素的最大宽度。 所以,在下面的图片中,黑框是具有固定宽度的容器div。 红色框表示容器div的内容,这些内容可能会发生变化。 我想查找红框的宽度,只使用js中的黑框。
这里是我一直在努力/尝试的jsfiddle:
我试过的当前jquery函数没有成功:
.width(); .innerWidth(); .outerWidth(); .scrollLeft();
注:我不知道这个容器的内容。 它们可以是任何html元素或html元素的组合,从div到img到iframe。 我可以放一个没有固定宽度的“红色盒子”。 黑盒子的溢出将被隐藏。
更新:容器中可能有任何数量的孩子。 像这样: http : //jsfiddle.net/w87k5/3/
更新2:我将对所有答案运行基准速度测试,以查看哪一个最快,然后选择一个。 感谢您的输入!
基准测试:我用0和100之间的随机宽度生成了1000个div,记录了Date().getTime(),做了测试,然后再次记录了时间。 结果如下:
〜2418平均 毫秒与for循环的长度。 不知怎的,我可能会搞砸了这个吗?
for (var i = 1; i <= count; i++){ var q = $("#box :nth-child(" + i + ")").width(); if(q > box){ box = q; } }
〜34.4平均。 毫秒为.each循环。
〜22.4平均。 ms。为.map函数。 (选择答案。)
I couldn't find any other questions asking the same thing, though that may be a problem with my search phrasing.
I'm trying to figure out how to find the largest width of all elements contained inside of a container div with a fixed width. So, in the image below, the black box is the container div with a fixed width. The red box represents the contents of the container div, which are subject to change. I want to find the width of the red box, using only the black box in js.
Here is a jsfiddle with what I've been working on/trying:
the current jquery functions I've tried, with no success:
.width(); .innerWidth(); .outerWidth(); .scrollLeft();
Note: I do not know ANYTHING about the contents of this container. They could be any html element or mix of html elements, from divs to imgs to iframes. I could put a "red box" without a fixed width surrounding them. Overflow of the black box will be hidden.
Update: There could be any number of children in the container. Like this: http://jsfiddle.net/w87k5/3/
Update 2: I'm going to run benchmark speed tests on all of the answers to see which one is the fastest, and select one after that. Thanks for all your input!
Benchmarks: I generated 1000 divs with a random width of inbetween 0 and 100, recorded the Date().getTime(), did the test, then recorded time again. Here are the results:
~2418 avg. milliseconds with the for loop for length. I might have messed this one up somehow?
for (var i = 1; i <= count; i++){ var q = $("#box :nth-child(" + i + ")").width(); if(q > box){ box = q; } }
~34.4 avg. ms for the .each loop.
~22.4 avg. ms for the .map function. (Chosen answer.)
原文:https://stackoverflow.com/questions/20767994
最满意答案
如果您知道要显示的对象,可以使用
$firebaseObject
而不是$firebaseArray
:var ref = firebase.database().ref('/myusers/').child("users"); $scope.user = $firebaseObject(ref.child($scope.id));
然后在您的HTML中显示它:
<div > <h2>{{user.firstname}}</h2> <p>{{user.age}}</p> </div>
If you know what object you want to display, you can use
$firebaseObject
instead of$firebaseArray
:var ref = firebase.database().ref('/myusers/').child("users"); $scope.user = $firebaseObject(ref.child($scope.id));
And then display it in your HTML with:
<div > <h2>{{user.firstname}}</h2> <p>{{user.age}}</p> </div>
相关问答
更多-
你似乎混淆了两个概念: object.$id .AngularFire对象的object.$id包含Firebase数据库中该对象的键。 Firebase术语中的firebaseUser.uid是Firebase身份验证用户的标识。 通常将Firebase身份验证用户存储在他们的uid下的数据库中,在这种情况下, user.$id将是他们的uid 。 但它们本质上仍然是不同的东西。 Users uid1 displayName: "makkasi" uid2 displa ...
-
默认情况下,这是不可能的,因为用户ID不是通过界面公开的,也不能在过滤器或段中使用(您可以在Dimensions和Metric Explorer中检查UI和API的可用字段)。 您可以将用户ID存储为自定义维度,然后按此过滤。 这对于已经收集的数据不起作用。 Per default this is not possible, since the user id is not exposed via the interface and cannot be used in filters or segments ...
-
在angularfire 1.0.0中通过独特的firebase id获取物品(Fetching item by unique firebase id in angularfire 1.0.0)[2022-12-27]
我知道$ getRecord()函数的文档有些误导。 你真正从$firebaseArray得到的是一个数组的承诺 。 这意味着您的posts变量将在未来的某个时间点包含您的帖子。 这就是说,看起来$getRecord函数只在promise已经解析时才起作用,也就是说,当数组已经从Firebase下载时。 为了确保在调用$getRecord函数时可以解决承诺问题,可以在承诺中使用$loaded() : var posts = $firebaseArray(ref); posts.$loaded().then( ... -
在新的AngularFire API更改时,项目关键信息不再是“免费”的。 您可以使用'.snapshotChanges()'来代替使用'.valueChanges()'来引用Observable。 .snapshotChanges() .map(changes => { return changes.map(change => ({key: change.payload.key, ...change.payload.val()})); }) 从那时起,您可以使用'it ...
-
如果您知道要显示的对象,可以使用$firebaseObject而不是$firebaseArray : var ref = firebase.database().ref('/myusers/').child("users"); $scope.user = $firebaseObject(ref.child($scope.id)); 然后在您的HTML中显示它:
{{user.firstname}}
{{user.age}}
-
(Angularfire)使用id从firebase数据库中检索特定数据((Angularfire) Retrieve specific data from firebase database with id)[2023-12-28]
更改: this.items=this.blogService.getObjectById(params['id']); 至: this.blogService.getObjectById(params['id']).subscribe( i => { this.items = i; }) 他们在getObjectById(id)中: getObjectById(id) { return this.afs.collection('collectionName').do ... -
Oracle解决方案(不确定它是否符合“一个简单的单个查询”的标准): WITH t AS (SELECT item_id, row_number() OVER (ORDER BY some_criteria DESC) rn FROM orders) SELECT t2.rn, t2.item_id FROM t t1 JOIN t t2 ON (t2.rn > t1.rn) WHERE t1.item_id = 145; 我的假设是没有重复item_id的值。 试图用MySQL术语来说,这 ...
-
根据文档,使用的哈希算法是MD5。 MD5散列函数用于将分区键映射到128位整数值,并使用分片的散列键范围将关联的数据记录映射到碎片。 见http://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecord.html 在您的情况下,如果您知道要为其标识相应分片的分区键,则需要执行以下两项操作: 计算分区键的MD5哈希 查看分片列表以查找散列键范围包含第一步中计算的散列值的分片。 这里有一些代码片段可以帮助您: MD5散列为BigIntege ...
-
按ID筛选出JIRA票证(Filter out JIRA tickets by id)[2022-10-28]
id字段是表示数据库中问题的数字,您要做的是使用key ,例如: key != MYPROJ-1234 那应该有用。 作为旁注,您可以通过执行以下操作查看问题的ID: /rest/api/2/issue/MYPROJ-1234 在JSON Repsonse中,您将看到: { "id": "82351", "key": "MYPROJ-1234", ... } 所以,如果你真的想,你可以这样做: id != 82351 The id field is the number th ... -
不应该是filter(item => item.food_group_id === ? Should't it be filter(item => item.food_group_id ===?