最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery getting value from dynamic array - Stack Overflow

programmeradmin2浏览0评论

I have an array with divs ids (in my case its all divs ID values od parent div (#area) ):

jQuery.fn.getIdArray = function () {
 var ret = [];
 $('[id]', this).each(function () {
  ret.push(this.id);
 });
 return ret;
 };

var array = $("#area").getIdArray();

I need to get an array field value, something like this:

var lef = $("#array".[0]).css("left");

I have an array with divs ids (in my case its all divs ID values od parent div (#area) ):

jQuery.fn.getIdArray = function () {
 var ret = [];
 $('[id]', this).each(function () {
  ret.push(this.id);
 });
 return ret;
 };

var array = $("#area").getIdArray();

I need to get an array field value, something like this:

var lef = $("#array".[0]).css("left");
Share Improve this question edited Mar 11, 2013 at 14:46 minimal asked Mar 11, 2013 at 14:16 minimalminimal 4573 silver badges15 bronze badges 3
  • 2 I'm afraid your question is a bit too "minimal". ;-) What is getIdArray's return value? What do you mean you need to get an array field value (the phrase is fairly clear, but then the code after it doesn't seem to relate to it). – T.J. Crowder Commented Mar 11, 2013 at 14:18
  • This is how you use array $('#'+array[0]).css("left"); – hop Commented Mar 11, 2013 at 14:19
  • We still don't know what .getIdArray() returns. If you have a custom filter you can: $("#area").filter(function(){//return true if you want this one used}).css("left") to make your custom filter you can: – HMR Commented Mar 11, 2013 at 14:28
Add a ment  | 

4 Answers 4

Reset to default 4

Taking a wild swing at it (see my ment on the question):

var array = $("#area").getIdArray();
var lef=$("#" + array[0]).css("left");

That assumes that getIdArray returns an array of strings, where each string is an id value for a DOM element, and that you want to get the left value for the first of those elements.

So for instance, if the array es back as:

["foo", "bar", "charlie"]

then the selector created by "#" + array[0] is #foo, so you end up getting the left value for the foo element.

If you have an actual JS array within your variable array just use bracket notation to access each individual ID.

// I have the # before-hand since I'm assuming you have just the ID name
var lef = $('#' + array[0]) // this will access the 1st one in the array

I think you are looking for this :

var divYouWantToChange = $("#"+array[0]);

I try to formulate this as an answer because getIdArray is not a jquery function and we don't know what it does. If you'd like to apply a custom filter to the $("#area") collection you can do so using filter. This will return a jquery object where you can get the .css("left") from.

If you'd like to save both the id's and the left property you can do so with the following code:

var objects=[];
$("#area").filter(function(){
  $this=$(this);//cache the object
  objects.push({id:$this.attr("id"),
   left:$this.css("left")
  };
});
console.log(objects);
发布评论

评论列表(0)

  1. 暂无评论