【发布时间】:2014-09-13 09:07:24
【问题描述】:
我试图在网站上实现字体大小调整功能。我编写了一些 jQuery 代码,它们在 Mozila 上运行完美,但在 Chrome、Safari 等其他浏览器上却无法运行。在 Chrome 和 Safari 上,它仅在第一次单击时调整字体大小。这是我的代码。
JQuery 代码
jQuery(document).ready(function(){
jQuery('[id="fontlarge"]').click(function(){
jQuery('*').each(function(){
var fontSize = jQuery(this).css('font-size');
fontSize = fontSize.substr(0,2);
//alert(fontSize);
if(fontSize<=13)
{
var currentSize = jQuery(this).css('font-size');
var currentSize = parseFloat(currentSize)+.2;
jQuery(this).css('font-size', currentSize);
}
});
});
jQuery('[id="fontsmall"]').click(function(){
jQuery('*').each(function(){
var fontSize = jQuery(this).css('font-size');
fontSize = fontSize.substr(0,2);
//alert(fontSize);
if(fontSize>=12)
{
var currentSize = jQuery(this).css('font-size');
var currentSize = parseFloat(currentSize)-.2;
jQuery(this).css('font-size', currentSize);
}
});
});
});
HTML 代码
<span id="fontsmall" style="cursor:pointer;">-</span> <span id="fontlarge" style="cursor:pointer;">+</span>
我不确定如何解决此问题。
【问题讨论】:
-
不确定这是否有帮助,因为它似乎是特定于浏览器的,但是......而不是
[id="fontlarge"],您可以使用#fontlarge。与[id="fontsmall"]和#fontsmall相同。 -
@DevlshOne DOM 元素选择器不是问题,因为我可以通过两种选择器 #fontlarge 和 [id="fontlarge"] 触发事件
标签: javascript jquery google-chrome safari browser