简体   繁体   English

对 html 标签内的单词进行排序忽略它们

[英]Sort words inside html tags ignoring them

I have some buttons on my web page which are looking like this:我的网页上有一些按钮,它们看起来像这样:

<button>Apple</button>
<button>Dog</button>
<button>Text</button>
<button>Boy</button>
<button class='whatNot'>Text</button>
<button class='smth whatNot'>Applepie</button>
<button class='whatNot'>Doge is barking</button>
<button class='smth'>Such a doge</button>
<button class='smth whatNot'>Wow!</button>
<button class='whatNot smth'>Pure delicious</button>
<button>Peach</button>
<button>Trip</button>

Now I want to sort these buttons in alphabetical order without loosing tags and classes.现在我想在不丢失标签和类的情况下按字母顺序对这些按钮进行排序。 I thought about making that in js: adding all button's names in array and then sort it.我想在 js 中做到这一点:将所有按钮的名称添加到数组中,然后对其进行排序。 But how will I restore their classes afterwards?但是之后我将如何恢复他们的课程? So, essentially, I just want to sort lines by words inside tags , but to not lose classes of each button during the sorting.所以,本质上,我只想按标签内的单词对行进行排序,但在排序过程中不要丢失每个按钮的类。 Hope, I was clear enough.希望,我已经足够清楚了。

Possible solution:可能的解决方案:

1) Load all buttons into an array of objects , ie: 1)将所有按钮加载到一个对象数组中,即:

[
    ...
    {
        sortValue: 'Text',
        html: '<button class='whatNot'>Text</button>'
    },
    {
        sortValue: 'Applepie',
        html: '<button class='smth whatNot'>Applepie</button>'
    },
    ...
]

2) Sort this object by 'sortValue' field 2) “sortValue”字段对此对象进行排序
3) Remove buttons from page 3)从页面中删除按钮
4) Append buttons from sorted object ('html' field) 4)从排序对象('html'字段)附加按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM