具有多个参数的jQuery自定义函数不起作用

时间:2018-12-18 07:58:59

标签: javascript jquery function

我试图创建JQUERY来将div附加到现有的div

(function($){
  $.fn.alertme=function(){
    var opts = $.extend( {}, $.fn.alertme.defaults, options );
    $divContent =$('<div></div>').appendTo(opts.container);
    $divContent.prop('class','divclass');
  };

  $.fn.alertme.defaults = {
    container: "body",
    background: "yellow"
  };
})(jQuery);

这就是我的称呼:

$(document).ready(function(){
  $('#testbutton').on('click',function(){
    $.alertme(
      {container : '#target'}
    );
  });
});

其返回错误选项未定义

请帮助我在这里我做错了

1 个答案:

答案 0 :(得分:3)

将选项作为参数传递。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<div class="col-lg-12 clearfix">
    <div id="hidden-xs">
    TEst data TEst data TEst data TEst 
    data TEst data TEst data TEst data TEst data TEst data TEst data TEst data TEst data 
    TEst data TEst data TEst data TEst data TEst data TEst data TEst data
    TEst data TEst data
    TEst data TEst data
    TEst data TEst data TEst data TEst data TEst data

    TEst data TEst data TEst data TEst data
		
                                TEst data TEst data TEst data TEst 
    data TEst data TEst data TEst data TEst data TEst data TEst data TEst data TEst data 
    TEst data TEst data TEst data TEst data TEst data TEst data TEst data
    </div>
    <div class="readmorehxs" id="dvReadmore"><b>Read more..</b></div>
    <div class="readmorehxs" id="dvShowless" style="display:none;text-align:right;"><b>..Show less</b></div>
</div>