如何在开发jQuery插件时更改HTML模板

时间:2016-02-16 10:10:13

标签: javascript jquery html jquery-plugins

假设我使用以下模板使用jQuery制作弹出插件。以下代码位于默认选项中的jQuery插件文件中。

<div class="popup">
    <div class="title"></div>
<div>

所以,我想根据用户传递的参数更改title

$('#test').pop_up({
    'title': 'this title i want to use'
});

完整代码

(function($){
    $.fn.extend({ 
        pop_up: function(options) {

            //Settings list and the default values
            var defaults = {
                template: '<div class="popup"><div class="title"></div></div>',
                title: 'Hello',
                content: 'Are you sure to continue?'
            };
            
            var options = $.extend(defaults, options);
            $(this).each(function () {
                var $this = $(this);
                $this.on('click', function (e) {
                    e.preventDefault();
                  $(options.template).find('.title').html(options.title); //here 
                });
            });
        }
    });
})(jQuery);

1 个答案:

答案 0 :(得分:0)

 <!-- Include jQuery Popup Overlay -->
  <script src="http://vast-eng.github.io/jquery-popup-overlay/jquery.popupoverlay.js"></script>

  <script>
    $(document).ready(function() {
      // Initialize the plugin
      $('#my_popup').popup();
    });
  </script>

尝试此js以便于弹出窗口找到更多Click here