Jquery - 按子级的innerHTML对DIV进行排序
我的 html 看起来像这样:
<div id="sortThis">
<div id="1">Price:<span class="price">20</span><span class="style">blue</span></div>
<div id="2">Price:<span class="price">23</span><span class="style">red</span></div>
<div id="3">Price:<span class="price">10</span><span class="style">red</span></div>
<div id="4">Price:<span class="price">29</span><span class="style">green</span></div>
<div id="5">Price:<span class="price">35</span><span class="style">blue</span></div>
</div>
我希望能够按 .price 或 .style 排序
我怀疑这篇文章部分回答了我的问题: 根据属性“data-sort”对 Jquery 中的 Div 进行排序?
这个插件几乎可以满足我的需求(因为它可以处理子属性)但它似乎没有达到子级的innerHTML: http://tinysort.sjeiti .com/
任何帮助都会受到这个新手的赞赏。
I've got html that looks something like this:
<div id="sortThis">
<div id="1">Price:<span class="price">20</span><span class="style">blue</span></div>
<div id="2">Price:<span class="price">23</span><span class="style">red</span></div>
<div id="3">Price:<span class="price">10</span><span class="style">red</span></div>
<div id="4">Price:<span class="price">29</span><span class="style">green</span></div>
<div id="5">Price:<span class="price">35</span><span class="style">blue</span></div>
</div>
And I want to be able to sort by .price or by .style
I suspect that this post partially answers my question: Sort Divs in Jquery Based on Attribute 'data-sort'?
And this plugin comes close to doing what I need (since it can handle child attributes) but it does not seem to go as far as children's innerHTML: http://tinysort.sjeiti.com/
Any help will be appreciated by this noob.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要直接使用子值而不使用插件进行这种排序,您可以使用类似的方法:
然后可以按价格排序:
该函数被参数化,以便可以轻松地与其他 div 和不同的排序键重用。
这是一个演示: http://jsfiddle.net/tc5dc/
使用tinysort插件
或者,如果您可以从以下提供的功能中受益tinysort 插件(有问题中提到),您可以动态增强您的 div 以适应该插件。
看看这个演示:http://jsfiddle.net/6guj9/
在示例中,我们首先添加
价格 和
style
值作为持有 div 的数据属性:然后我们可以自由地使用tinysort 对相关属性进行排序。按价格排序很简单:
只需传入不同的配置对象即可更改排序顺序和键。链接的演示展示了一种实现此目的的方法,但您可能可以想出更好的方案来满足您的需求。
To do this sort directly using the child values and without a plugin, you could use something like:
Sorting by price can then be done as such:
The function is parametrised so that it can be easily reused with other divs and different sort keys.
Here's a demo: http://jsfiddle.net/tc5dc/
Using the tinysort plugin
Alternatively, if you can benefit from the features provided by the tinysort plugin (mentioned in question), you could dynamically augment your divs to suit the plugin.
Check out this demo: http://jsfiddle.net/6guj9/
In the example, we first add the
price
andstyle
values as data attributes of the holding div:We're then free to use tinysort to sort on the relevant attributes. Sorting by price would be simply:
Changing the sort order and keys can be done by simply passing in different config objects. The linked demo shows one way to do this, but you can probably come up with a better scheme to suit your needs.
你可以用 jquery 进行排序,
如果你这样做的话,它会自动按价格排序
我会看看我是否不能用 if 划出一些东西:)
得到类似的东西:
You can sort with jquery by doing
It automaticly sort by price if you do
Ill see if i cant scratch something out with an if :)
Got something like:
使用我的 jquery 插件 SortContent :
您可以使用助手根据您想要的合适内容进行排序。
如果你想颠倒顺序,请将
false
设置为asc
选项另请注意,你可以直接选择 div 的子级而不是父级: 这是相同的:
DEMO :
http://jsfiddle.net/abdennour/ytmry/
Use my jquery plugin SortContent :
You can use helper to sort according to the suitable conten that you want.
if you want to reverse order ,set
false
toasc
optionAnother note, you can select directly children of div instead of parent: It is the same :
DEMO :
http://jsfiddle.net/abdennour/ytmry/
标记答案的第一部分排序不正确,我使用 1 - 100 之间的数字进行测试,它误解了 10 以下的任何内容。原因是它适用于文本值和 ASCII 代码,而不是实际的数值。
在这里,我在排序之前使用 parseInt() 将文本值转换为整数。
那我们就叫它一样
The first part of the marked answer does not sort correctly, I tested it using numbers from 1 - 100 and it misinterpreted anything under 10. The reason being it works on a text value and the ASCII code instead of the actual numeric value.
Here I have converted the text values to Integers using parseInt() before sorting.
Then we call it the same
这对我来说是工作。
下面的代码根据价格文本将 div 从低到高缩短。
this was work for me.
This bellow code short the div in low to high base on the price text.