Style a certain character in a string

前端 未结 4 1307
名媛妹妹
名媛妹妹 2021-01-26 00:37

I want to style a certain character in a string via jQuery, but have no clue how to approach that. I have the following situation



        
4条回答
  •  无人共我
    2021-01-26 01:07

    My own approach would be:

    $('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,'' + aKey + '');
            // 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 demo.

    References:

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

提交回复
热议问题