Jquery css font-family不能使用引号 [英] Jquery css font-family not working with quotes

查看:502
本文介绍了Jquery css font-family不能使用引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用2个字体,并希望使用Jquery在他们之间切换,事情是我的字体的名字是'ArvoBold',当我使用

  css('font-family','ArvoBold')

它更改为不存在的字体ArvoBold(无引号)!
我试图使它工作与

  css(font-family,'ArvoBold' )

但它似乎不工作,我也试图创建一个变量



  var fontvar ='+ArvoBold+' 
css('font-family',fontvar);

,并且没有成功的原因在元素的CSS(使用google chrome的检查器检查)



任何人都可以帮助我吗?



提前感谢! / p>

John

解决方案

不确定您的确切实施,通过转义字符串中的单引号:

  $(。font-face ,\'ArvoBold\');为什么需要单引号?


p>快速测试在内容div中转义单引号输出ArvoBold:

 <!DOCTYPE html& 
< html lang =en>
< head>
< meta charset =utf-8>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.jstype =text / javascript>< / script>
< style>
#content {border:solid 1px red; width:100px; height:100px; text-align:center;}
< / style>
< / head>

< body>

< div id =content>< / div>

< script type =text / javascript>
$(document).ready(function(){
$(#content)。html(\'ArvoBold \');
});
< / script>

< / body>
< / html>


I'm using 2 font-faces and wish to switch between them using Jquery, thing is my font-face's name is 'ArvoBold' and when I use the

css('font-family','ArvoBold') 

it changes to a non existing font named ArvoBold (no-quotes)! I've tried to make it work with

css("font-family","'ArvoBold'") 

but it doesn't seem to work, I've also tried to create a variable

var fontvar="'"+"ArvoBold"+"'";
css('font-family',fontvar);

and also with no success 'cause on the css of the element (checked using google chrome's inspector) it keeps accusing ArvoBold instead of 'ArvoBold'.

Can anybody help me out?

Thanks in advance!

John

解决方案

Not sure of your exact implementation, but this works by escaping the single quotes in the string:

$(".font-face").css("font-family","\'ArvoBold\'");

Why do you need the single quotes?

Quick Test on escaping the single quotes outputs 'ArvoBold' in the content div:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>   
<style>
#content{border:solid 1px red;width:100px;height:100px;text-align:center;}
</style>
</head>

<body>

<div id="content"></div>

<script type="text/javascript">
$(document).ready(function(){
$("#content").html("\'ArvoBold\'");
});
</script>

</body>
</html>

这篇关于Jquery css font-family不能使用引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆