且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

设置字符串中某个字符的样式

更新时间:2023-02-26 11:30:03

我自己的方法是:

$('a[accesskey]').each( //selects only those a elements with an accesskey attribute
    function(){
        var aKey = $(this).attr('accesskey'); // finds the accesskey value of the current a
        var text = $(this).text(); // finds the text of the current a
        var newHTML = text.replace(aKey,'<span class="access">' + aKey + '</span>');
        // creates the new html having wrapped the accesskey character with a span
        $(this).html(newHTML); // sets the new html of the current link with the above newHTML variable
    });

JS Fiddle演示.

参考文献:

  • has-attribute selector.
  • attr().
  • replace()
  • html()