首页 > 解决方案 > 在 jquery 结果中创建自定义函数未定义

问题描述

我正在学习如何在 jquery 中创建自定义函数。我在互联网上看到如何创建自定义函数并自行测试。我在下面有这段代码作为我的测试,但不幸的是我的自定义函数与.repeat函数不同。我打算在这里创建就像是重复功能。

$(document).ready(function() {
  console.log = function(text) {
    $('#console').append($('<div>').text(text));
  };

  $.fn.duplify = function(count) {
    return count;
    //return this.each(function() {
    //Do stuff
    //});
  };

  try {
    console.log('hello'.repeat(2));
    console.log('hello'.duplify(3));
  } catch (err) {
    console.log(err);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="console"></div>

标签: javascriptjquery

解决方案


推荐阅读