使用元素文本作为CSS font-family

时间:2014-01-21 01:10:32

标签: javascript jquery

小提琴 - http://jsbin.com/emerinA/1/edit

我正在尝试使用位于.grab-typography div中的锚点文本,并将其用作该单个锚点的css字体系列。

JQuery的/使用Javascript:

$('.grab-typography').find('a').css('font-family', $(this).text());

这是html。

<div class="grab-typography"><br>
  <a class="grab-serif" href="javascript:void(0)">serif</a>
  <a class="grab-sans" href="javascript:void(0)">sans</a>
  <a class="grab-arial" href="javascript:void(0)">arial</a>
  <a class="grab-arial-black" href="javascript:void(0)">arial black</a>
  <a class="grab-courier" href="javascript:void(0)">courier</a>
  <a class="grab-comic-sans-ms" href="javascript:void(0)">comic sans ms</a>
  <a class="grab-helvetica" href="javascript:void(0)">helvetica</a>
  <a class="grab-impact" href="javascript:void(0)">impact</a>
  <a class="grab-lucida-sans" href="javascript:void(0)">lucida sans</a>
  <a class="grab-tahoma" href="javascript:void(0)">tahoma</a>
  <a class="grab-times-new-roman" href="javascript:void(0)">times new roman
  </a>
  <a class="grab-verdana" href="javascript:void(0)">verdana</a>
</div>

html如果我添加font-face html。 (我想尽可能避免这种情况)

<div class="grab-typography"><br>
  <a class="grab-serif" href="javascript:void(0)" title="serif"><font face="serif">serif</font></a>
  <a class="grab-sans" href="javascript:void(0)" title="sans"><font face="sans">sans</font></a>
  <a class="grab-arial" href="javascript:void(0)" title="arial"><font face="arial">arial</font></a>
  <a class="grab-arial-black" href="javascript:void(0)" title="arial black"><font face="arial black">arial black</font></a>
  <a class="grab-courier" href="javascript:void(0)" title="courier"><font face="courier">courier</font></a>
  <a class="grab-comic-sans-ms" href="javascript:void(0)" title="comic sans ms"><font face="comic sans ms">comic sans ms</font></a>
  <a class="grab-helvetica" href="javascript:void(0)" title="helvetica"><font face="helvetica">helvetica</font></a>
  <a class="grab-impact" href="javascript:void(0)" title="impact"><font face="impact">impact</font></a>
  <a class="grab-lucida-sans" href="javascript:void(0)" title="lucida sans"><font face="lucida sans">lucida sans</font></a>
  <a class="grab-tahoma" href="javascript:void(0)" title="tahoma"><font face="tahoma">tahoma</font></a>
  <a class="grab-times-new-roman" href="javascript:void(0)" title="times new roman"><font face="times new roman">times new roman</font>
  </a>
  <a class="grab-verdana" href="javascript:void(0)" title="verdana"><font face="verdana">verdana</font></a>
</div>

我没有太多运气。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

试试这个:

$('.grab-typography').find('a').css('font-family', function () {
    return $(this).text()
});

<强> jsFiddle example

答案 1 :(得分:-1)

你可以抓住标签之间的文字吗?

$('.grab-typography').find('a[title="' + $(this).text() + '"]').text();

这样您就不必担心添加标签

测试示例:http://jsfiddle.net/ktTh8/