4

I create eight divs in a for loop, and I want to add links each div to a different html page. And I also name the html page from 0~7. So how can I make the variable "i" into the href? Thanks!

for(i = 0; i < 8; i++) {
    counter[i] = increase * (i * 2);
    var div = $('<div class="node"><a id="link" href=i + ".html"><img src=archive/' + i + '.jpg width="200" height="200"/></a></div>');
    div.css({'top':yoffset,'left':innerWidth/8 * i});
    $('body').append(div);
    objects[i] = div;
}
4

3 に答える 3

4

変化する

var div = $('<div class="node"><a id="link" href=i+".html"><img src=archive/'+i+'.jpg width="200" height="200"/></a></div>');

var div = $('<div class="node"><a id="link" href="'+i+'.html"><img src=archive/'+i+'.jpg width="200" height="200"/></a></div>');
于 2012-11-27T15:58:48.760 に答える
2
var div = $('<div class="node"><a id="link" href="' + i + '.html"><img src=archive/' + i + '.jpg width="200" height="200"/></a></div>');

これにより、各リンクが<i>.htmlを指すようになります。

于 2012-11-27T15:59:01.460 に答える
1

I would reccomend creating the elements and setting the attributes instead of creating HTML code for the elements:

for(i = 0; i < 8; i++) {
  counter[i] = increase * (i * 2);
  var img = $('<img>', { src = 'archive/' + i + '.jpg', width: '200', height: '200' });
  var a = $('<a>', { id: 'link', href: i + '.html' }).append(img);
  var div = $('<div>', { 'class': 'node' }).append(a).css({'top':yoffset,'left':innerWidth/8 * i});
  $('body').append(div);
  objects[i] = div;
}
于 2012-11-27T16:07:19.293 に答える