Home  >  Article  >  Web Front-end  >  12 Ways to Solve Common Browser Compatibility Issues with Javascript_Javascript Tips

12 Ways to Solve Common Browser Compatibility Issues with Javascript_Javascript Tips

WBOY
WBOYOriginal
2016-05-16 18:37:141249browse

如果你不知道原因,不要过于担心,请研究CSS规则并查看这篇文章:使用CSS来修正一切: 20 +常见错误和修复
如果这些也无效,您可以通过下面列出的12个javascript解决方案修复它,这样您的网页看起来就能跨越所有浏览器了!
在本文中,我们会揭开你在开发web应用是可能会遇到的12个最常见的CSS问题的javascript解决方案。

1. 自动匹配高度

javascipt-自动匹配高度

自从我们抛弃了基于Table的页面布局后,创建同等高度栏目或内容盒子的视觉效果已然是一个挑战。

1.1 用jQuery设置匹配高度

这个jQuery插件在同一个容器里“平衡”盒子的高度并创造一个简介的网格——几乎从可用性和性能的角度使用简单的JavaScript替代: equalHeights()函数测定一个容器里的所有同级元素同容器的高度,然后设置每个元素的最低高度为最高的元素的高度。

如何工作

equalHeights()通过循环测定指定元素的最高级别的子节点,然后设置他们的最小高度值为最高的元素的高度。

点击这里预览效果

1.2 用jQuery匹配栏目高度

jQuery的另一个可以使盒子的高度相等的插件

$(“#col1, #col2″).equalizeCols();

将如你所想的那样匹配高度

$(“#col1, #col2″).equalizeCols(“p,p”);

匹配这两卷,并在#col1或#col2(短的那个)里的P标签后面添加空白.
2. IE6 PNG透明支持

IE6以下的版本不支持png透明。使用hack,IE 5.5和6也已经可以支持,但hack并不理想的且难以使用。让我们来看看我们能做些什么来支持IE6用户 ,同时为网站的大多数访客带来最佳的透明效果。

2.1 强制IE6支持透明

IE7的是一个Dean Edwards建立的JavaScript库,以强迫MSIE(IE6,IE5)表现的像一个兼容标准的浏览器。它修复许多CSS问题并使透明PNG在IE6和IE5下正常工作,它还允许高级的CSS选择器。

点击查看预览效果
点击下载源文件

2.2. 改良iFixPng

javascript-IE6-透明图片

修正IE6及以下的PNG图片的问题,IMG标签和CSS背景图片都可以。这个插件是对原始iFixPng插件的一种改进。特点包括:图像或有背景图片的标签,现在支持background-position,其中包括IE浏览器的绝对定位的修正。(bottom: -1px || bottom: 0px)

点击查看预览效果
点击这里下载源文件

3. 用Javascript改变class

这是一个方便的JavaScript函数,可以在当前的文件的任何元素的class由oldClass改为newClass。这是特别有用的快速的利用CSS而不是用编码改变风格。

function changeClass(oldClass, newClass) {
      var elements = document.getElementsByTagName(“*”);
      for( i = 0; i < elements.length; i++ ) {
if( elements[i].className == oldClass ) elements[i].className = newClass;
}
}

点击查看预览效果
点击下载源文件

4. CSS浏览器选择器

如果您可以只需键入一个特殊选择器,在这里您可以写一些JavaScript ,设置一个Class在基于当前的浏览器的名字的标签会怎么样?

javascript-css-选择器

4.1 CSS Browser

这是一个非常小的javascript只有一行,而且不到1kb,它允许CSS选择器。它让您可以为每个操作系统和每个浏览器写具体的CSS代码。你可以写一些JavaScript ,设置Class的名字,也就是说,内容根据当前的浏览器。

点击查看预览效果
点击下载源文件

jQuery 浏览器选择器

这里有另外一个基于jQuery的非常简单的处理浏览器选择器的方法,你需要做的只是加载jQuery库文件,并添加下面的一块儿代码。

$(document).ready(function(){
$(‘html').addClass($.browser);
});

现在你可以准备你的样式,如.msie,.mozilla, .opera, .safari 或其它目标浏览器。

点击查看预览效果

5. 最小/最大 高度/宽度支持

针对CSS min-width, min-height, max-width, max-height, border-*-width, margin, 和padding 属性,这里有一些很好的jQuery修正。

5.1 jQMinMax

这是一个为没有原声的支持min-width, max-width,min-height和max-height的地方添加支持的jQuery插件。

点击查看预览效果
点击下载源文件

5.2 JSizes

这个小jQuery插件为CSS min-width, min-height, max-width, max-height, border-*-width, margin, 和padding 属性添加支持。特别是他提供一种方法来确定一个元素在那里可见。由于所有的型号的方法返回数值,所以这些也可以安全的使用在严格的DOM元素方面。

jQuery(function($) {
var myDiv = $(‘#myDiv');

// set margin-top to 100px and margin-bottom to 10em
myDiv.margin({top: 100, bottom: ‘10em'});

// displays the size of the top border in pixels
alert(myDiv.border().top);

// displays true if the element is visible, false otherwise
alert(myDiv.isVisible());

// set padding-right to 10px and margin-left to 15px using chaining
myDiv.padding({right: 10}).margin({left: 15});
});

点击查看预览效果
点击下载源文件

6. 元素垂直/水平居中

你可能之前遇到过这个问题:水平或垂直居中某个元素。垂直居中在CSS里面相当麻烦,特别是你想支持所有主流浏览器。

javascrpt-垂直居中

6.1 Center element plugin

这个插件可以使页面中的所有元素居中,垂直和水平居中采用css负margin的方法。

$(“element”).center(); //vertical and horizontal
$(“element”).center({
horizontal: false // only vertical
});

Click to view the preview effect
Click to download the source file

6.2 How do I vertically center an element?

In this video tutorial, Jeffrey Jordan Way will show you how to use the power of jQuery combined with CSS to vertically center an image in your browser.

7. Use Q tag in IE

People expect to use the Q tag instead of the blockquote tag to display quotes. However, IE/Win does not support the Q tag. Because of this, most website authors choose not to use the Q tag.

7.1 QinIE

When you add this script at the head of your file, IE automatically scans web pages for Q tags and displays them correctly (including nested references). When (if) IE supports Q tags in the future, this plugin will add browser version checking.

Click to download the source file

8. Increase click target size and get more response conversions

javascript-js

Say goodbye to boring “read more…” links by putting all your content into a clickable tab.

Click to download the source file

9. Lazy loader

Lazy loader is a jQuery. It can delay loading of images inside the page. It will not be loaded until the user browses the image outside the field of view (the visible part of the page). This is exactly the opposite of image preloading.

Click to view the preview effect
Click here to download the source file

10. bgiframe

Easily solve the z-index problem under IE.

javascript-bgiframe

Click to view the preview effect
Click here to download the source file

11. ieFixButtons

ieFixButtons is a jquery plug-in that fixes the bug of the

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn