【发布时间】:2017-06-04 15:19:44
【问题描述】:
我想创建动态切换器并使用 jquery 和一些动态变量创建按钮对象然后我希望 Html 对象附加到 Html 我不能这样做下面的链接是我的代码 访问jsfiddle.net/eu3a0k3m/2/
我的js代码:
var count = 3;
$("#btn").click(function(){
CreateInlineChildWithSwitchery(++count);
});
function CreateInlineChildWithSwitchery (countOfChilds){
var Integer_LastSwitcheryId = count;
var btn = $('<input type="checkbox" id="js-switch' + Integer_LastSwitcheryId + '" class="js-switch' + Integer_LastSwitcheryId + ' js-check-change' + Integer_LastSwitcheryId + ' col-lg-3" data-switchery="true">');
console.log(btn)
$('#all-encyclopedia').append(
$("#tpl ").html()
.replace(/{switchery}/g,btn)
);
eval("elem" + Integer_LastSwitcheryId.toString() + "=document.querySelector('#js-switch"+Integer_LastSwitcheryId.toString()+"')");
eval("switchery" + Integer_LastSwitcheryId.toString() +"={elem" + Integer_LastSwitcheryId.toString() + ", color: '#1AB394',secondaryColor: '#688ab0'}");
eval("changeCheckbox" + Integer_LastSwitcheryId.toString() + "=document.querySelector('.js-check-change"+Integer_LastSwitcheryId.toString()+"')");
eval("changeField" + Integer_LastSwitcheryId.toString() + "=document.querySelector('.js-check-change-field"+Integer_LastSwitcheryId.toString()+"')");
}
for(i=1;i<4;i++)
CreateInlineChildWithSwitchery(count++);
我的 HTML 代码:
<script type="text/tpl" id="tpl">
{switchery}
</script>
<button id="btn">insert Switchery</button>
<div id="all-encyclopedia"></div>
【问题讨论】:
-
嗨 alireza,您需要在 Stack Overflow 上的帖子中包含代码 - 不要只链接到第三方网站。