1

I'm working with Twitter Bootstrap 2.2.2 and jQuery 1.7.2 trying to use anchors with a flat button style as tags. Such tags are dynamically added after saving a text input; I'm using a very simple HTML structure:

<p>
    <a class="btn btn-small"><span>Tag 1</span></a>
    <a class="btn btn-small"><span>Tag 2</span></a>
</p>

With jQuery I just create a new a with the required attributes and values, and it is then attached to the paragraph.

However, whenever you create more than 1, the spacing between elements gets all messed up and new tags are created without spacing between them.

I created the following fiddle to demo my problem: http://jsfiddle.net/marcelo1251/WeuLb/

Click Add Tag more than 2 times and you'll see the problem...

Is there a way to fix the spacing with the dynamically generated elements?

Thanks, Marcelo C.

4

1 回答 1

0

我会调查问题,无论如何,如果你这样做

 var tagClone = $('.template').html();
    $('p#target').append(tagClone);

有用。

从技术上讲,它们并不相同:

    //your code
    var tagClone = $('.template').clone(); //tagClone is a jQuery object
    $('p#target').append(tagClone.find('a'));

创建一个 JQUERY OBJECT 并将其附加到目标。

    //my code
    var tagClone = $('.template').html(); //tagClone is a string
    $('p#target').append(tagClone);

获取 html 内容作为 A STRING 并将其附加到目标。append 函数 ( http://api.jquery.com/append/ ) 但是可以接受 jquery 对象或 html 字符串,因此它应该可以工作。

于 2013-02-05T02:31:04.730 回答