-1

这里我有一个代码,当我点击按钮时,我必须将新的 div ID 添加到 div ID 底部,但我不知道我错在哪里:

contentStr += '<button class="dodaj">Add to timeline</button></div>';

$(".dodaj").click(function() {
  $('#bottom').append($('<div id="new">'+place.name+'</div>').css('marginLeft',100));
});

$(contentStr).dialog({ modal:true });

所以首先我在模态窗口 jquery UI 中调用 contentStr ,在这个模态窗口中我必须调用带有类 .dodaj 的按钮并进入 ID 底部以添加新的 div ID

那么这里有什么问题呢?

4

3 回答 3

3

使用.on()

由于您button是动态添加的,因此无法直接访问,因此您必须使用Event delegation.

$(document).on("click", ".dodaj", function () {
    $('#bottom').append($('<div id="new">' + place.name + '</div>').css('marginLeft', 100));
});
于 2013-10-07T14:43:55.653 回答
0

$('<div id="new">'+place.name+'</div>')一定是问题所在。

如果你<div id="new">的页面中真的有一个,你应该得到它$("#new")

我认为您想要的是删除 de $() 并内联设置边距属性:

append('<div id="new" style="margin-left:100;">content</div>')
于 2013-10-07T14:47:18.680 回答
0

试试这个动态添加的元素,将它绑定到最近的静态父元素

$(document).on('click', '.dodaj', function () {
    $('#bottom').append($('<div id="new">' + place.name + '</div>').css('marginLeft', 100));
});
于 2013-10-07T14:43:53.680 回答