javascript - 安装显示更多或更少脚本时未捕获的 ReferenceError
问题描述
我有以下来自jquery 脚本演示的代码,用于在页面上显示更多或更少的文本:
<script src="js/show-hide-text.js"></script>
<script>
var th = new showHideText('.my-message', {
charQty : 250,
ellipseText : "...",
});
</script>
<script type="text/javascript">
我将他们的 js 文件包含show-hide-text.js
在我自己的 js 文件编译中。我按照他们的示例将类添加到 html 中<div class="my-message">content content content</div>
。然后我通过将其添加到我的 js 文件中来调用它:
/**
* 2007-2017 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2017 PrestaShop SA
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
import UIkit from 'uikit';
import Icons from 'uikit/dist/js/uikit-icons.min';
import './plugins/jquery.lettering';
import './plugins/jquery.simpleTicker';
import './plugins/show-hide-text';
import 'bootstrap-touchspin';
import './checkout';
import './customer';
import './listing';
import './product';
import './cart';
import Form from './components/form';
import ProductMinitature from './components/product-miniature';
import ProductSelect from './components/product-select';
import prestashop from 'prestashop';
import EventEmitter from 'events';
import './lib/bootstrap-filestyle.min';
import './lib/jquery.scrollbox.min';
import './components/block-cart';
// "inherit" EventEmitter
for (var i in EventEmitter.prototype) {
prestashop[i] = EventEmitter.prototype[i];
}
window.UIkit = UIkit;
UIkit.use(Icons);
$(document).ready(() => {
//let dropDownEl = $('.js-dropdown');
const form = new Form();
//let topMenuEl = $('.js-top-menu ul[data-depth="0"]');
//let dropDown = new DropDown(dropDownEl);
//let topMenu = new TopMenu(topMenuEl);
let productMinitature = new ProductMinitature();
let productSelect = new ProductSelect();
//dropDown.init();
form.init();
//topMenu.init();
productMinitature.init();
productSelect.init();
//Lettering
$(".fancy-price").lettering();
//Category Page Toggle Filter Button Class and Icon for Mobile
$('.green-btn').click(function() {
var $span = $(this).find(".toggle-icon");
if ($span.attr("uk-icon") === "chevron-down") {
$span.attr("uk-icon", "chevron-up")
} else {
$span.attr("uk-icon", "chevron-down")
}
$(this).toggleClass("green-btn red-btn");
});
//Toggle Filter Button class and icon
// jQuery.simpleTicker - Slide Effect
$.simpleTicker($("#ticker-slide"),{'effectType':'slide'});
// Show and Hide More Texts
var th = new showHideText('.minimize-text', {
charQty : 100,
ellipseText : "...",
moreText : "Read more",
lessText : "Read less"
});
});
但它没有奏效。相反,我得到了Uncaught ReferenceError: showHideText is not defined message。我没有在遵循他们的例子时错过了什么
解决方案
如果您使用的是 jquery,它应该可以工作,那么您必须包括
1 - jquery.js
2- show-hide-text.js
我刚刚禁用了代码片段中的其他脚本,以向您展示它在您的代码中的工作方式,这些脚本应该在jquery.js
$(".fancy-price").lettering();
$.simpleTicker($("#ticker-slide"),{'effectType':'slide'});
$(document).ready(() => {
// jQuery.simpleTicker - Slide Effect
// $.simpleTicker($("#ticker-slide"),{'effectType':'slide'});
//Lettering
// $(".fancy-price").lettering();
// Show and Hide More Texts
var th = new showHideText('.my-message', {
charQty : 100,
ellipseText : "...",
moreText : "Read less",
lessText : "Read more"
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.jqueryscript.net/demo/Show-Hide-Long-Text-Max-Length-jQuery/js/show-hide-text.js"></script>
<div class="my-message">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium, cumque, modi. Sint dolore, fugiat asperiores, fuga nulla commodi delectus natus, beatae porro earum dicta. Culpa blanditiis quis adipisci recusandae mollitia.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium, cumque, modi. Sint dolore, fugiat asperiores, fuga nulla commodi delectus natus, beatae porro earum dicta. Culpa blanditiis quis adipisci recusandae mollitia.
</div>
推荐阅读
- html - 如何正确定义具有角度分割区域的页面的高度?
- javascript - .JSON 中的值路径
- java - 如何自动扩展 Java War 部署设置
- session - 客户名称未出现,产品未显示在迷你购物车,Magento 2
- python - 可重复使用的功能
- python - 返回两个元组时如何仅使用一个参数
- php - 获取整个数据时如何停止在ajax中获取数据
- swift - 如何隐藏列表中的空行
- java - java.sql.SQLSyntaxErrorException:ORA-01722:invalid number_where IN
- python - OpenCV imshow函数在jupyter笔记本中显示黑色图像