Kir*_*eck 6 javascript css jquery internet-explorer-7 internet-explorer-6
有没有办法使这个解决方案IE6和IE7兼容?
http://jsfiddle.net/kirkstrobeck/sDh7s/1/
我想我找到了一个真正的解决方案.我把它变成了一个新功能:
jQuery.style(name, value, priority);
你可以用它来获取值.style('name')
一样.css('name')
,获得与CSSStyleDeclaration上.style()
,并设置值-有能力来指定优先级为"重要".请参阅https://developer.mozilla.org/en/DOM/CSSStyleDeclaration.
var div = $('someDiv');
console.log(div.style('color'));
div.style('color', 'red');
console.log(div.style('color'));
div.style('color', 'blue', 'important');
console.log(div.style('color'));
console.log(div.style().getPropertyPriority('color'));
Run Code Online (Sandbox Code Playgroud)
这是输出:
null
red
blue
important
Run Code Online (Sandbox Code Playgroud)
// For those who need them (< IE 9), add support for CSS functions
var isStyleFuncSupported = CSSStyleDeclaration.prototype.getPropertyValue != null;
if (!isStyleFuncSupported) {
CSSStyleDeclaration.prototype.getPropertyValue = function(a) {
return this.getAttribute(a);
};
CSSStyleDeclaration.prototype.setProperty = function(styleName, value, priority) {
this.setAttribute(styleName,value);
var priority = typeof priority != 'undefined' ? priority : '';
if (priority != '') {
// Add priority manually
var rule = new RegExp(RegExp.escape(styleName) + '\\s*:\\s*' + RegExp.escape(value) + '(\\s*;)?', 'gmi');
this.cssText = this.cssText.replace(rule, styleName + ': ' + value + ' !' + priority + ';');
}
}
CSSStyleDeclaration.prototype.removeProperty = function(a) {
return this.removeAttribute(a);
}
CSSStyleDeclaration.prototype.getPropertyPriority = function(styleName) {
var rule = new RegExp(RegExp.escape(styleName) + '\\s*:\\s*[^\\s]*\\s*!important(\\s*;)?', 'gmi');
return rule.test(this.cssText) ? 'important' : '';
}
}
// Escape regex chars with \
RegExp.escape = function(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
// The style function
jQuery.fn.style = function(styleName, value, priority) {
// DOM node
var node = this.get(0);
// Ensure we have a DOM node
if (typeof node == 'undefined') {
return;
}
// CSSStyleDeclaration
var style = this.get(0).style;
// Getter/Setter
if (typeof styleName != 'undefined') {
if (typeof value != 'undefined') {
// Set style property
var priority = typeof priority != 'undefined' ? priority : '';
style.setProperty(styleName, value, priority);
} else {
// Get style property
return style.getPropertyValue(styleName);
}
} else {
// Get CSSStyleDeclaration
return style;
}
}
Run Code Online (Sandbox Code Playgroud)
有关如何读取和设置CSS值的示例,请参阅https://developer.mozilla.org/en/DOM/CSSStyleDeclaration.我的问题是我已经!important
在我的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:
.container {
font-size:100%;
}
p {
font-size:1em;
}
Run Code Online (Sandbox Code Playgroud)
JS
function changeFontSize(n)
{
var size = $('.container').data('size');
size += n * 10;
$('.container').css('font-size',size+'%').data('size',size);
}
$(document).ready(function(){
$('body').prepend(' \
<div class="font-size-changer"> \
<a href="#" class="decrease">A↓</a> \
<!--<a href="#" class="reset">A</a>--> \
<a href="#" class="increase">A↑</a> \
<a href="#" class="null">null</a> \
</div> \
').find('> .container').data('size',100);
$('.font-size-changer .increase').click(
function()
{
changeFontSize(1);
}
);
$('.font-size-changer .decrease').click(
function()
{
changeFontSize(-1);
}
);
});
Run Code Online (Sandbox Code Playgroud)
我已将保存删除到cookie部分,但这很容易重新应用..
一个技巧是在某处保存初始百分比(我使用data()),因为如果你尝试用.css('font-size')检索它,它会给你计算的大小(例如'16px').可能有一种方法可以将值作为百分比获得但不记得如何.
重新应用cookie保存部分时,请记住将初始data()设置为cookie中的值而不是100%,然后调用changeFontSize(0)来应用它.
无论如何,这段代码适用于IE6.