如何禁用 jQuery 脚本,如果 IE8.

How to disable a jQuery script if IE8

本文关键字:如果 IE8 脚本 何禁用 jQuery      更新时间:2023-09-26

我将网站不同元素的所有jQuery代码都放在一个称为脚本的文件中.js但是如果用户使用IE8,我希望禁用一个代码块。这就是我要禁用的:

    $(document).ready(function(){
 $('.parallax-block-1').parallax("50%", 0.1);
});

到处查找,我唯一发现的就是在 html 代码中<script> </script>之前放置一个 IF 条件注释,例如 - <!--[if !(IE 8)]><!-->,但由于我将所有内容都放在一个 js 文件中,IF 语句将禁用其中的所有其他脚本。

希望我的问题有意义。谢谢大家!

您可以使用

<!--[if !(IE 8)]><!-->将类名插入到<html>标签中,然后在 js 文件中使用该类名来指定是否启用 js 函数。例如:在 HTML 文件中:

 <!--[if IE 8]> <html class="lt-ie9"> <![endif]--> /* Rendered in IE8 browser */
<!--[if gt IE 8]><!--> <html> <!--<![endif]--> /* Rendered in other browsers */

然后在您的 JS 文件中:

if ($("html.lt-ie9").length > 0) {
   //your functions or code that should work on all browsers except for IE8 goes here
}

希望对您有所帮助。