bla < /s开发者_开发百科pan> blalba < span id=\"myID > bla< /span> and so on..." />
开发者

JQuery iterate through string occurrences

I've an HTML like this

blalblala < span id="myID" >bla < /s开发者_开发百科pan> blalba < span id="myID > bla< /span> and so on...

Now I would to get the left position of all items of kind myID. I've tried with this code

var x = "";   
$('.myID').each(function(index) { 
    x += " " + $(this).position().left+",";  
}); 
return x;

But it's wrong because it does not return anything. What's wrong? Thanks


You should not use id on more than one element. The appropriate attribute to use is class.

<span class="myClass" >bla < /span> 
<span class="myClass"> bla< /span>

And then your code should work if you update the selector to be:

$('.myClass')


var x = new Array;
$('.myID').each(function(index) { x[index] = $(this).position().left; });
console.log(x);

try that and see what is present in the array x.


  • you have multiple identical IDs in your document [fragment]
  • you are using the class selector, while you have IDs

To quote the freenode#css bot:

Explore the intricacies of HTML and CSS here: http://wsc.opera.com/ , http://www.htmlhelp.com/ , http://css-discuss.incutio.com/ , http://www.brainjar.com/ , http://www.htmldog.com/ , http://css.maxdesign.com.au/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜