简体   繁体   English

从 jQuery 中的多个相同类名获取值 CSS

[英]Get value CSS from multiple same class name in jQuery

How to Get value CSS from multiple same class name in jQuery?如何从jQuery中的多个相同类名获取值CSS?

That is, I have many elements that want to be entered into the database,也就是说,我有很多元素要输入到数据库中,

Each of these elements has different CSS values,每个元素都有不同的 CSS 值,

For example the CSS value of each element is 'background-image',例如每个元素的 CSS 值为 'background-image',

I want to take the value of each element and input into the database using Ajax request,我想获取每个元素的值并使用 Ajax 请求输入到数据库中,

Question, how to take CSS value from each data on that element?问题,如何从该元素的每个数据中获取 CSS 值?

Use this simple Example使用这个简单的例子

 $('.content').each(function() { $('.result').html('isi1: ' + $(this).css('width') + ' ==== isi2: ' + $(this).css('width')); });
 span.content { display: inline-block; height: 100px; background: #666; } span.isi1 { width: 100px; } span.isi2 { width: 200px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="id_1"> <span class="content isi1">ISI1</span> <span class="content isi2">ISI2</span> </div> <p class="result"></p>

Edit on https://jsfiddle.net/FIERMANDT/b1pq3p3z/https://jsfiddle.net/FIERMANDT/b1pq3p3z/ 上编辑

Edit 21 Jul 2020 VanilaJS version编辑 2020 年 7 月 21 日 VanilaJS 版本

 /*for loop and getComputedStyle() method*/ var getEachCSSVal = document.querySelectorAll('.content'); for (var i = 0; i < getEachCSSVal.length; i++) { var cssVal = window.getComputedStyle(getEachCSSVal[i]).getPropertyValue('width'); console.log(cssVal) }
 span.content { display: inline-block; height: 100px; background: #666; } span.isi1 { width: 100px; } span.isi2 { width: 200px; }
 <div id="id_1"> <span class="content isi1">ISI1</span> <span class="content isi2">ISI2</span> </div> <p class="result"></p>

The problem with your code is that you "overwrite" the value with last iteration from the each loop...你的代码的问题是你用每个循环的最后一次迭代“覆盖”了值......

$(this) is the actual element inside the loop. $(this)是循环内的实际元素。

See how the each() function works.看看each()函数是如何工作的。 in the callback you can get the index and then you kind of know where you are if you append() html content to an element.在回调中,您可以获得index ,然后如果将 append() html 内容附加到元素,您就会知道自己在哪里。

 $('.content').each(function(index){ $( ".result" ).append( "<div>isi"+index+": "+$(this).css('width')+"</div>" ); });
 span.content { display: inline-block; height: 100px; background: #666; } span.isi1 { width: 100px; } span.isi2 { width: 200px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="id_1"> <span class="content isi1">ISI1</span> <span class="content isi2">ISI2</span> </div> <p class="result"></p>

Simple Console简单的控制台

 $('.content').each(function(index){ console.log($(this).css('width')); });
 span.content { display: inline-block; height: 100px; background: #666; } span.isi1 { width: 100px; } span.isi2 { width: 200px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="id_1"> <span class="content isi1">ISI1</span> <span class="content isi2">ISI2</span> </div> <p class="result"></p>

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

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