Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何向单击其他按钮时创建的按钮添加样式?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何向单击其他按钮时创建的按钮添加样式?

Javascript 如何向单击其他按钮时创建的按钮添加样式?,javascript,jquery,html,Javascript,Jquery,Html,我有一个按钮,每当你点击另一个按钮时,它就会被加载(所以它不会在启动时加载,而我不做任何事情) 现在我的问题是,无论何时加载此按钮,我/我应该如何使此函数运行?我相信您可以创建一个css类示例: .buttonStyle{ background-color: red }; 然后你可以得到你的按钮并添加这个类 var button = document.getElementById("testbtn"); button.className = button.className + " butto

我有一个按钮,每当你点击另一个按钮时,它就会被加载(所以它不会在启动时加载,而我不做任何事情)


现在我的问题是,无论何时加载此按钮,我/我应该如何使此函数运行?

我相信您可以创建一个css类示例:

.buttonStyle{ background-color: red };
然后你可以得到你的按钮并添加这个类

var button = document.getElementById("testbtn");
button.className = button.className + " buttonStyle";

使用jQuery,您可以执行以下操作:

$( "button" ).addClass( "myClass yourClass" );

按钮
标签不支持
onload
,因此您需要作为其他答案提示或使用

document.onload =function(){

  //change the css of that button or call function you want 

}

您可以使用jQuery的事件委托。使用这种技术,加载DOM后是否生成按钮并不重要

$('body').on('click', '.generated_btn', function() {
    //here you can change css
});
事件委派允许我们将单个事件侦听器附加到 父元素,该元素将为匹配 选择器,无论这些子体现在存在还是添加到 未来


更多信息:

Jquery允许还是仅允许javascript?因此您有
btnA
,当您单击它时,它会生成
btnB
,然后您想在创建
btnB
时触发事件?为什么不在
文档上执行此操作。onload
?一切都一样
document.onload =function(){

  //change the css of that button or call function you want 

}
$('body').on('click', '.generated_btn', function() {
    //here you can change css
});