onclick $(document.body).append()不起作用

时间:2013-12-26 07:49:18

标签: javascript jquery twitter-bootstrap

这里我想在onc​​lick事件上显示bootstrap模态。 Onclick事件alert()即将到来,但 $(document.body).append()(模态代码)似乎没有初始化...我在控制台中也没有收到任何错误... 这是我的代码......

    (function() {  
tinymce.create('tinymce.plugins.wpc', {  
    init : function(ed, url) { 
        ed.addButton('wpc', {  
            title : 'Add Contact Us form',  
            image : url+'/dd_note.gif', 
            onclick : function() {
                alert("hii"); // it's coming on onclick event

                $(document.body).append('<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title" id="myModalLabel">Forms List</h4> </div> <div class="modal-body"> <script> showForms("'+url+'"); </script></div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button data-dismiss="modal" type="button" class="btn btn-primary" onclick="addForm()">Add Page</button> </div> </div> </div> </div>');

                $('#myModal').modal();
            }  
        });  
    },  
    createControl : function(n, cm) {  
        return null;  
    },  
});  
tinymce.PluginManager.add('wpc', tinymce.plugins.wpc);  
})();

有人能告诉我我的代码出了什么问题吗?

提前致谢

2 个答案:

答案 0 :(得分:1)

在你的html中有脚本标签,有些浏览器不允许将其添加为文本,因此你必须从javascript创建脚本dom元素,然后附加它或尝试这样的事情:

替换你的附加html:

<script> showForms("'+url+'"); </script>

为:

<script> showForms("'+url+'");</' + 'script>

允许浏览器知道它是脚本标记。

工作demo以附加此html

答案 1 :(得分:0)

试试这个

(function() {  
tinymce.create('tinymce.plugins.wpc', {  
    init : function(ed, url) { 
        ed.addButton('wpc', {  
            title : 'Add Contact Us form',  
            image : url+'/dd_note.gif', 
            onclick : function() {
                alert("hii"); // it's coming on onclick event

                $('body').append('<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title" id="myModalLabel">Forms List</h4> </div> <div class="modal-body"> <script> showForms("'+url+'"); </script></div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button data-dismiss="modal" type="button" class="btn btn-primary" onclick="addForm()">Add Page</button> </div> </div> </div> </div>');

                $('#myModal').modal('show');
            }  
        });  
    },  
    createControl : function(n, cm) {  
        return null;  
    },  
});  
tinymce.PluginManager.add('wpc', tinymce.plugins.wpc);  
})();