javascript 浏览器后退按钮上未定义 slimScroll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22395281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:03:33  来源:igfitidea点击:

slimScroll is not defined on Browser Back Button

javascriptjqueryslimscroll

提问by The01Geek

I have a basic code that uses the jquery.slimscroll, which works perfectly fine on page load. But if I navigate to an inner page and try to go back using the "back" button on browser, I get the following error:

我有一个使用 jquery.slimscroll 的基本代码,它在页面加载时工作得很好。但是,如果我导航到内部页面并尝试使用浏览器上的“后退”按钮返回,则会出现以下错误:

Uncaught TypeError: Object [object Object] has no method 'slimScroll'

未捕获的类型错误:对象 [object Object] 没有方法“slimScroll”

I have checked the element's lengthand confirmed that the object exists. My jQuert is as simple as this:

我检查了元素的长度并确认对象存在。我的jQuery就像这样简单:

<script type="text/javascript">
    $("#boxsrchome").slimScroll();
</script>

Note: I tried wrapping the code around $(document).ready()and $(window).load(). And that did not help either.

注意:我尝试将代码包裹在$(document).ready()$(window).load()周围。这也没有帮助。

采纳答案by Alain Jacomet Forte

First, see if jQuery is defined by inputting jQuery in the console. If it is, try this: Try wrapping it in:

首先通过在控制台输入jQuery查看是否定义了jQuery。如果是,请尝试以下操作:尝试将其包装在:

(function($) {
    $("#boxsrchome").slimScroll();
})(jQuery);

If it isn't, you have a problem with the way you're including your files. Also, make sure you aren't including slimscroll.js in the wrong place, and make sure you're not redefining the jQuery object by using jQuery.noConflict();anywhere, although, that shouldn't matter with this method.

如果不是,则您包含文件的方式有问题。此外,请确保您没有在错误的位置包含 slimscroll.js,并确保您没有通过jQuery.noConflict();在任何地方使用来重新定义 jQuery 对象,尽管这与此方法无关。

回答by Felix

Seem like the browser cannot load your slimScrollplugin due to wrong path of your file or wrong file name. Try to use firebugor chrome developer tools to check if your file are loaded properly.

slimScroll由于文件路径错误或文件名错误,浏览器似乎无法加载您的插件。尝试使用firebug或 chrome 开发人员工具来检查您的文件是否正确加载。

You can also try to use direct link from Github, so your code should look like:

您也可以尝试使用 Github 的直接链接,因此您的代码应如下所示:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://raw.github.com/rochal/jQuery-slimScroll/master/jquery.slimscroll.min.js"></script>
<script>
    jQuery(document).ready(function($) {
        $("#boxsrchome").slimScroll();
    });
</script>

回答by Vikas Rathod

Follow the below steps and it will work definitely.

按照以下步骤操作,它肯定会起作用。

1.use this npm: npm install jquery-slimscroll

1.使用这个npm: npm install jquery-slimscroll

2.Update Your .angular-cli.jsonfile with below code

2.使用以下代码更新您的.angular-cli.json文件

"scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/jquery-slimscroll/jquery.slimscroll.min.js"
      ],

3.html Structure

3.html结构

<div id="content">
<p>Lorem ipsum dolor sit amet, consectetur .... snip</p>
</div>

4.Use following slimscroll

4.使用以下slimscroll

$('#content').slimScroll({
        height: '250px',
        color:'#F44336'
    });