Javascript 点击按钮调用js代码

Javascript 点击按钮调用js代码,javascript,jquery,html,css,Javascript,Jquery,Html,Css,这里我有一个代码,当我点击按钮时,我必须将div ID new添加到div ID底部,但我不知道我错在哪里: contentStr += '<button class="dodaj">Add to timeline</button></div>'; $(".dodaj").click(function() { $('#bottom').append($('<div id="new">'+place.name+'</div>').c

这里我有一个代码,当我点击按钮时,我必须将div ID new添加到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 });
contentStr+=“添加到时间线”;
$(“.dodaj”)。单击(函数(){
$('#bottom').append($(''+place.name+'').css('marginLeft',100));
});
$(contentStr).dialog({modal:true});
所以首先,我在模式窗口jquery UI中调用contentStr,在这个模式窗口中,我必须使用class.dodaj调用按钮,并在ID底部添加新的div ID


那么这里有什么问题吗?

对于动态添加的元素,请尝试这样做,将其绑定到最近的静态父元素

$(document).on('click', '.dodaj', function () {
    $('#bottom').append($('<div id="new">' + place.name + '</div>').css('marginLeft', 100));
});
$(文档).on('click','.dodaj',函数(){
$('#bottom').append($(''+place.name+'').css('marginLeft',100));
});
使用

由于动态添加了
按钮
,因此无法直接访问,因此必须使用
事件委派

$(document).on("click", ".dodaj", function () {
    $('#bottom').append($('<div id="new">' + place.name + '</div>').css('marginLeft', 100));
});
$(document).on(“click”,“.dodaj”,函数(){
$('#bottom').append($(''+place.name+'').css('marginLeft',100));
});
$(''+地点.名称+'')
一定是问题所在

如果您的页面中确实有一个
,您应该使用
$(“#新建”)

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

append('<div id="new" style="margin-left:100;">content</div>')
append('content')

由于按钮是动态添加的,您需要像
$(document)一样的委托。在('click','.dodaj',function(){
上使用append,您应该附加一个字符串或DOM元素,而不是jquery对象。如果您想在以后更改其CSS,您应该使用.appendTo:
$('+place.name+'').appendTo('.#bottom bottom').CSS('marginLeft',100)
我不知道为什么不工作控制台中有错误吗?工作。。。。。。。。。。。thanks@jsdna欢迎乐意帮忙:)