我正在尝试通过触发click事件来修改div的css属性。出于某种原因,这种情况并没有发生,这让我发疯。你知道为什么会这样吗?
事件如下所示:
$("#colButton3").on("click", function() {
unCollapse('#CarouselSpace','#CarouselBody');
});
unCollapse功能是:
var unCollapse = function(headerElement, bodyElement) {
$(headerElement).css('margin-top', '1500px');
$(bodyElement).css('min-height', '820px');
};
按钮本身是用jquery生成的,但它的html是:
<button class="btn btn-success" href="#" id="colButton3" style="display: inline-block;">Learn More</button>
目标div是:
<div id="CarouselSpace" class="row"><h1 id="CarouselHeader"></h1></div>
<div id="CarouselBody" class="row"></div>
我做错了什么?
谢谢你们。
答案 0 :(得分:2)
动态元素需要在文档上绑定而不是元素本身,因为文档加载后元素被加载
static void Main(string[] args)
{
ThreadLocal<int> field = new ThreadLocal<int>(() => 0, true);
Thread firstThread = new Thread(new ThreadStart(() =>
{
field.Value = 999;
}));
Thread secondThread = new Thread(new ThreadStart(() =>
{
field.Value = 1000;
}));
firstThread.Start();
secondThread.Start();
firstThread.Join();
secondThread.Join();
IList<int> valueList = field.Values;
foreach (int arr in valueList)
Console.WriteLine(arr);
Console.Read();
}
&#13;
$(document).ready(function() {
$(document).on("click", "#colButton3", function() {
unCollapse('#CarouselSpace', '#CarouselBody');
});
});
var unCollapse = function(headerElement, bodyElement) {
$(headerElement).css('margin-top', '1500px');
$(bodyElement).css('min-height', '820px');
};
&#13;
#CarouselBody,
#CarouselSpace {
border: 1px solid #ff6600;
}
&#13;
答案 1 :(得分:0)
代码应该有效,您可能在创建按钮之前尝试绑定click事件。创建按钮后尝试使用$ .live或bind。