简体   繁体   English

Javascript / HTML分配值

[英]Javascript/HTML assigning a value

I am having a problem with some Javascript/HTML/CSS code. 我对某些Javascript / HTML / CSS代码有疑问。 I am fairly new to creating websites, so please bear with me. 我是刚创建网站的新手,所以请多多包涵。

what I am ultimately trying to do is pull a dynamic value from javascript and use that to sort some divs (in a container). 我最终想做的是从javascript中提取一个动态值,并使用它来对一些div(在容器中)进行排序。 What I am thinking is I will assign the value to the Id and then pull those into an array to be sorted by tinysort. 我在想的是,我将把值分配给Id,然后将其拉入数组以按tinysort排序。 If there's a faster way to do this, let me know. 如果有更快的方法,请告诉我。

However, my first problem is putting the data into the id so it can be sorted. 但是,我的第一个问题是将数据放入id中以便可以对其进行排序。 Would I do something like 我会做类似的事情吗

document.getElementById(namesort).value = iterator;

or would I use something like myData ? 还是我会使用myData东西?

Note: I don't want to display the value, I just want to use it to sort. 注意:我不想显示该值,我只想使用它进行排序。

Please ask for clarification if needed! 如果需要,请要求澄清! Thanks in advance. 提前致谢。 :) :)

Here is the applicable code to this problem. 这是适用于此问题的代码。 http://jsfiddle.net/dw77hLyp/1/ http://jsfiddle.net/dw77hLyp/1/

It just basically shows a very basic outline of some of my code. 它只是基本显示了一些代码的非常基本的概述。

Without JQuery. 没有JQuery。 You can create your attribute : 您可以创建属性:

 document.getElementById(namesort).createAttribute('myData');
     document.getElementById(namesort).setAttribute("myData","hello Im in");

Pure JS Solution: 纯JS解决方案:

for( i = 0 ; i < iterator.length ; i++ ) {
    document.getElementsByName('namesort')[i].setAttribute('data-sort',iterator[i]);
}

That way you add each of those elements an attribute called data-sort where the iterator as a value, then could create an array insert all the namesort elements to it, use the sorting algorithm and you're done. 这样,您可以为每个元素添加一个名为data-sort的属性,其中将iterator作为值,然后可以创建一个数组,向其中插入所有namesort元素,使用排序算法即可完成操作。

document.getElementById("divID").innerHTML="someContent";

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

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