这次给大家带来jQuery实现点击标题文字切换字体步骤详解,jQuery实现点击标题文字切换字体的注意事项有哪些,下面就是实战案例,一起来看一下。
这个主要通过判断被点击的元素的子元素中是否包含了b元素来进行字体的切换,其中wrapInner函数是为了在$author元素的内部添加b标签。
切换回正常字体是通过将内容转化为纯文本形式,再替换元素内容来实现的。
核心代码如下:
1 2 3 4 5 6 7 8 9 10 11 | $( '#f-author' ).css( 'cursor' , 'pointer' );
$( '#f-author' ).click( function (event){
var $author = $(this);
if (! $author .children().is( 'b' )){
$author .wrapInner( '<b></b>' );
}
else {
var text = $author .text();
$author .text(text);
}
});
|
Salin selepas log masuk
完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <meta charset= "utf-8" >
<title>www.jb51.net jQuery点击标题切换字体</title>
<script src= "https://libs.baidu.com/jquery/2.0.0/jquery.min.js" ></script>
<p id= "f-author" >这里显示测试标题文字</p>
<script>
$( '#f-author' ).css( 'cursor' , 'pointer' );
$( '#f-author' ).click( function (event){
var $author = $(this);
if (! $author .children().is( 'b' )){
$author .wrapInner( '<b></b>' );
}
else {
var text = $author .text();
$author .text(text);
}
});
</script>
|
Salin selepas log masuk
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
Vue实现PopupWindow组件使用步骤解析
JS实现透明度渐变功能
Atas ialah kandungan terperinci jQuery实现点击标题文字切换字体步骤详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!