Home > Web Front-end > JS Tutorial > How to create the effect of switching fonts by clicking on the title text

How to create the effect of switching fonts by clicking on the title text

php中世界最好的语言
Release: 2018-06-04 15:36:22
Original
2089 people have browsed it

This time I will show you how to make the effect of clicking on the title text to switch the font, and what are the precautions. Here is a practical case, let's take a look.

This mainly switches the font by determining whether the sub-element of the clicked element contains a b element. The wrapInner function is to add the b tag inside the $author element.

Switching back to the normal font is achieved by converting the content into plain text and then replacing the element content.

The core code is as follows:

$('#f-author').css('cursor','pointer');
$('#f-author').click(function(event){
  var $author = $(this);
  if(!$author.children().is('b')){//子元素没有b
    $author.wrapInner('<b></b>');//包含在$author里面
  }
  else{
    var text = $author.text(); //纯文本
    $author.text(text);
  }
});
Copy after login

The complete code is as follows:





www.jb51.net jQuery点击标题切换字体



这里显示测试标题文字

<script> $('#f-author').css('cursor','pointer'); $('#f-author').click(function(event){   var $author = $(this);   if(!$author.children().is('b')){//子元素没有b     $author.wrapInner('&lt;b&gt;&lt;/b&gt;');//包含在$author里面   }   else{     var text = $author.text(); //纯文本     $author.text(text);   } }); </script>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website !

Recommended reading:

How to solve the error in configuring the packaging file path

How to operate css files with r.js

The above is the detailed content of How to create the effect of switching fonts by clicking on the title text. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template