javascript - 在 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>
解决方案
推荐阅读
- html - SVG圆在旋转时弹跳
- visual-studio-code - 默认情况下,Video Studio Code 中终端中显示的输入/输出是否保存在任何地方?
- javascript - 未捕获的类型错误:无法读取 null 的属性“添加”-slick.js
- python - 使用一个热编码列参数创建箱线图
- html - 要输入的数据列表...在 vue 中没有数据列表
- twitter-bootstrap - ASP.Net 警报在几秒钟后消失
- ruby - 是否可以为返回多个值的方法设置多个默认参数?
- powerbi - 是否按设计在 Web 上发布时获取 powerbi 报告的页面名称?
- reactjs - 组件没有在它上面找到路由器上下文?
- java - Android中View Model类中的错误解决方法