单击时将字体更改为HTML未排序列表上的粗体 [英] changing the font to bold on a HTML unsorted list when clicked

查看:107
本文介绍了单击时将字体更改为HTML未排序列表上的粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML未排序列表,该列表捕获了其单击时"事件.单击列表项后,我想将该项目的字体设置更改为粗体,以便用户获得已被选中的视觉指示.这可能吗?

I have a HTML unsorted list which I capture its "on click" event. When a list item is clicked on I want to change that items font setting to bold so that the user gets a visual indicator that it’s been selected. Is this possible?

推荐答案

<li onclick="this.style.fontWeight= 'bold'">​​​​​​​​​​​​​​​​​​​​​​​​​​​

还是要在单击另一个li时将其更改回常规状态?我想您应该再使用jQuery(在常规javascript中是可能的,但这要容易得多)

Or do you want to change it back to regular when another li is clicked? I think you should use jQuery then (it's possible in regular javascript but this is just so much easier)

$('li').click(function () {
    $(this).siblings('li').css("fontWeight", "normal");
    $(this).css("fontWeight", "bold");
});​

甚至更简单,只需添加一个类:

Or even easier, just add a class:

CSS:.selected { font-weight: bold } jQuery:

CSS: .selected { font-weight: bold } jQuery:

$('li').click(function () {
    $('li.selected').removeClass('selected');
    $(this).addClass('selected');
});​

这篇关于单击时将字体更改为HTML未排序列表上的粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆