创建并附加一个元素,innerHTML?恐怖...[请不要使用jquery]
我有一个按字母顺序返回列表的脚本,如下所示
<div id="x">
<ul>
<li>Apple
<li>Banana
<li>Blackberry
<li>Blueberry
<li>Cherry
<li>Cranberry
</ul>
</div>
但是列表中有很多项目(几乎 100 个),我希望按如下方式重新排列它们
<div id="x">
<span id="A">A</span>
<ul>
<li>Apple
</ul>
<span id="B">B</span>
<ul>
<li>Banana
<li>Blackberry
<li>Blueberry
</ul>
<span id="C">C</span>
<ul>
<li>Cherry
<li>Cranberry
</ul>
<div>
顺便说一句,我并没有真正对水果进行排序,这这只是一个例子。
它必须采用这种形式,出于无关紧要的原因,我只需要帮助创建和附加 span 元素和单独的列表。 我尝试过使用内部/外部 HTML,但我发现它真的非常困难。我目前有这样的东西:
function getAllLists()
{
var page = document.getElementById("x")
var allLists = page.getElementsByTagName("li");
var num = allLists.length;
//Some form of a for counter loop here
for (var counter = 0; counter < num; counter++) {
var first=allLists[counter].outerHTML;
var second=allLists[counter+1].outerHTML;
var firstletter= (allLists[counter].substring(0, 1)).toUpperCase();
var secondletter= (allLists[counter+1].substring(0, 1)).toUpperCase();
allLists[counter].outerHTML = '<span id="'+firstletter+'">'+firstletter+'</span>'+first;
}
}
请避免使用 JQUERY。
我怎么强调都不为过!!
我不仅发现它非常难以理解,因为我仍然是 javascript 的业余爱好者,并且发现 js 已经足够困难了,jquery 的语法就像愚蠢地难以理解。
我确信无需使用 jquery 就可以实现我的目标。
有什么想法吗?所有帮助/评论/想法都非常感谢。
非常感谢。
I have a script that returns a list in alphabetical order as follows
<div id="x">
<ul>
<li>Apple
<li>Banana
<li>Blackberry
<li>Blueberry
<li>Cherry
<li>Cranberry
</ul>
</div>
However there are many items (almost 100) in the list and I am hoping to rearrange them as follows
<div id="x">
<span id="A">A</span>
<ul>
<li>Apple
</ul>
<span id="B">B</span>
<ul>
<li>Banana
<li>Blackberry
<li>Blueberry
</ul>
<span id="C">C</span>
<ul>
<li>Cherry
<li>Cranberry
</ul>
<div>
I'm not really sorting fruit btw, this is just an example.
It MUST be in that form, for reasons that matter not, I just need help creating and appending the span elements and the individual lists.
I have tried using inner/outer HTML and I'm finding it really really difficult. I currently have something like this:
function getAllLists()
{
var page = document.getElementById("x")
var allLists = page.getElementsByTagName("li");
var num = allLists.length;
//Some form of a for counter loop here
for (var counter = 0; counter < num; counter++) {
var first=allLists[counter].outerHTML;
var second=allLists[counter+1].outerHTML;
var firstletter= (allLists[counter].substring(0, 1)).toUpperCase();
var secondletter= (allLists[counter+1].substring(0, 1)).toUpperCase();
allLists[counter].outerHTML = '<span id="'+firstletter+'">'+firstletter+'</span>'+first;
}
}
PLEASE PLEASE PLEASE PLEASE AVOID USING JQUERY.
I can't stress this enough!!
Not only do I find it extremely difficult to understand, as I am still an amateur at javascript and find js already difficult enough as it is, the syntax for jquery is like just stupidly hard to understand.
I am certain it is possible to achieve what I am aiming for WITHOUT the need to use jquery.
Any ideas? all help/comments/ideas are more than appreciated.
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先忘记使用inner/outerHTML 操作DOM。它们可以方便地插入 HTML 块或文档部分,但它们绝对不适合一般的 DOM 操作。
使用 DOM 方法。
首先,将所有 LI 元素加载到一个数组中。然后使用排序函数对它们进行排序。然后将它们放回到用 UL 元素包裹的 DOM 中,并在每次第一个字母发生变化时用跨度分隔。
编辑
这是一个可以完成这项工作的函数。它对 lIs 进行排序,不确定是否真的需要。如果没有的话,这个功能就会变得简单很多。
请注意,LI 仅移动到文档片段,并且原始 UL 被多个元素替换。我将 LI 打乱以表明排序是有效的。
编辑 2
如果您将数组作为文本,则:
Firstly forget all about manipulating the DOM using inner/outerHTML. They are handy for inserting chunks of HTML or sections of documents, but they are definitely not intended for general DOM manipulation.
Use DOM methods.
Firstly, load all the LI elements into an array. Then sort them using a sort function. Then put them back into the DOM wrapped in UL elements and separated by spans each time the first letter changes.
Edit
Here's a function that does the job. It sorts the lIs, not sure if that's really needed. If not, the function becomes a lot simpler.
Note that the LIs are just moved to the document fragment and that the original UL is replaced by multiple elements. I've jumbled the LIs to show that the sort works.
Edit 2
If you have the array as text, then:
HTML:
JavaScript:
例子:
Live Code
HTML:
JavaScript:
Example:
Live Code
好吧,这就是我完成它的方法:
请在此处查看此工作: http://jsfiddle.net/HjWhY/
Well, this is the way I get it done:
See this working here: http://jsfiddle.net/HjWhY/