javascript – 这个解决方案可以兼容IE7和IE6吗?

前端之家收集整理的这篇文章主要介绍了javascript – 这个解决方案可以兼容IE7和IE6吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法使这个解决方案IE6和IE7兼容?

http://jsfiddle.net/kirkstrobeck/sDh7s/1/

this question起拉

我想我找到了一个真正的解决方案.我把它变成了一个新功能

jQuery.style(name,value,priority);

您可以使用.style(‘name’)获取值,就像.css(‘name’),使用.style()获取CSSStyleDeclaration,并设置值 – 可以将优先级指定为’important’ .见https://developer.mozilla.org/en/DOM/CSSStyleDeclaration.

演示

  1. var div = $('someDiv');
  2. console.log(div.style('color'));
  3. div.style('color','red');
  4. console.log(div.style('color'));
  5. div.style('color','blue','important');
  6. console.log(div.style('color'));
  7. console.log(div.style().getPropertyPriority('color'));

这是输出

  1. null
  2. red
  3. blue
  4. important

功能

  1. // For those who need them (< IE 9),add support for CSS functions
  2. var isStyleFuncSupported = CSSStyleDeclaration.prototype.getPropertyValue != null;
  3. if (!isStyleFuncSupported) {
  4. CSSStyleDeclaration.prototype.getPropertyValue = function(a) {
  5. return this.getAttribute(a);
  6. };
  7. CSSStyleDeclaration.prototype.setProperty = function(styleName,priority) {
  8. this.setAttribute(styleName,value);
  9. var priority = typeof priority != 'undefined' ? priority : '';
  10. if (priority != '') {
  11. // Add priority manually
  12. var rule = new RegExp(RegExp.escape(styleName) + '\\s*:\\s*' + RegExp.escape(value) + '(\\s*;)?','gmi');
  13. this.cssText = this.cssText.replace(rule,styleName + ': ' + value + ' !' + priority + ';');
  14. }
  15. }
  16. CSSStyleDeclaration.prototype.removeProperty = function(a) {
  17. return this.removeAttribute(a);
  18. }
  19. CSSStyleDeclaration.prototype.getPropertyPriority = function(styleName) {
  20. var rule = new RegExp(RegExp.escape(styleName) + '\\s*:\\s*[^\\s]*\\s*!important(\\s*;)?','gmi');
  21. return rule.test(this.cssText) ? 'important' : '';
  22. }
  23. }
  24.  
  25. // Escape regex chars with \
  26. RegExp.escape = function(text) {
  27. return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&");
  28. }
  29.  
  30. // The style function
  31. jQuery.fn.style = function(styleName,priority) {
  32. // DOM node
  33. var node = this.get(0);
  34. // Ensure we have a DOM node
  35. if (typeof node == 'undefined') {
  36. return;
  37. }
  38. // CSSStyleDeclaration
  39. var style = this.get(0).style;
  40. // Getter/Setter
  41. if (typeof styleName != 'undefined') {
  42. if (typeof value != 'undefined') {
  43. // Set style property
  44. var priority = typeof priority != 'undefined' ? priority : '';
  45. style.setProperty(styleName,priority);
  46. } else {
  47. // Get style property
  48. return style.getPropertyValue(styleName);
  49. }
  50. } else {
  51. // Get CSSStyleDeclaration
  52. return style;
  53. }
  54. }

有关如何读取和设置CSS值的示例,请参见https://developer.mozilla.org/en/DOM/CSSStyleDeclaration.我的问题是我已经设置了!对我的CSS中的宽度很重要以避免与其他主题CSS发生冲突,但是我对jQuery中的宽度所做的任何更改都不会受到影响,因为它们会被添加到style属性中.

兼容性

为了使用setProperty函数进行优先级设置,http://help.dottoro.com/ljdpsdnb.php表示支持IE 9和所有其他浏览器.我已经尝试使用IE 8并且它已经失败了,这就是为什么我在我的函数中构建了对它的支持(参见上文).它将适用于使用setProperty的所有其他浏览器,但它需要我的自定义代码才能在< IE 9.

解决方法

这看起来不必要复杂..我只是使用基于em的字体大小的容器内的标签,并使用百分比调整容器的字体大小.这样,容器内的所有标签都会自动调整大小.

JsFiddle:http://jsfiddle.net/qqxe9/

CSS:

  1. .container {
  2. font-size:100%;
  3. }
  4.  
  5. p {
  6. font-size:1em;
  7. }

JS

  1. function changeFontSize(n)
  2. {
  3. var size = $('.container').data('size');
  4. size += n * 10;
  5. $('.container').css('font-size',size+'%').data('size',size);
  6. }
  7.  
  8.  
  9. $(document).ready(function(){
  10. $('body').prepend(' \
  11. <div class="font-size-changer"> \
  12. <a href="#" class="decrease">A&darr;</a> \
  13. <!--<a href="#" class="reset">A</a>--> \
  14. <a href="#" class="increase">A&uarr;</a> \
  15. <a href="#" class="null">null</a> \
  16. </div> \
  17. ').find('> .container').data('size',100);
  18. $('.font-size-changer .increase').click(
  19. function()
  20. {
  21. changeFontSize(1);
  22. }
  23. );
  24. $('.font-size-changer .decrease').click(
  25. function()
  26. {
  27. changeFontSize(-1);
  28. }
  29. );
  30. });

我已将保存删除到cookie部分,但这很容易重新应用..

一个技巧是在某处保存初始百分比(我使用data()),因为如果你试图用.css(‘font-size’)检索它,它会给你计算的大小(例如’16px’).
可能有一种方法可以将值作为百分比获得但不记得如何.

重新应用cookie保存部分时,请记住将初始data()设置为cookie中的值而不是100%,然后调用changeFontSize(0)来应用它.

无论如何,这段代码适用于IE6.

猜你在找的JavaScript相关文章