[英]Using jQuery to change style of clicked element and removing that style from other elements with same class
EDIT: fixed it! 编辑:修复它! Thanks to those who said to post my HTML - I looked at it, and didn't realize that I had the "appname" class twice.
感谢那些说发布我的HTML的人-我看着它,却没有意识到我有两次“ appname”类。
<li class="<?php echo $apid; ?> appname appname_getinfo appname_<?php echo $apid; ?>">
<span class="appname"><?php echo $apname; ?></span>
</li>
So, I removed the "" and it works! 因此,我删除了“”,它起作用了!
Say that I have three elements with a class: 假设我上课有三个要素:
EL ONE EL ONE
EL TWO 两次
EL THREE 三三
When I click on EL ONE, I would like to make it bold. 当我单击EL ONE时,我想使其加粗。 When I click on EL TWO, it should become bold and EL ONE should become normal weight.
当我单击EL TWO时,它应该变成粗体,而EL ONE应该变成正常的重量。 When I click on EL THREE next, it should become bold and EL TWO becomes normal weight.
当我接下来单击EL THREE时,它应该变成粗体,而EL TWO 2变成正常的重量。
MY SCRIPT: 我的脚本:
//// app info ////
$("li.appname_getinfo").click(function(){
var appID = this.className.split(' ')[0];
$.get("wishlist/appinfo.php?pw=R32kd63DL&apid=" + appID, function(data){
$("div#appinfo").html(data);
});
$("div#appinfo").show();
$(".appname").css("font-weight", "normal");
$(this).css("font-weight", "bold");
});
I thought that I could set all with class "appname" to normal, then change $(this) to bold, but it doesn't work. 我以为可以将“ appname”类的所有内容都设置为正常,然后将$(this)更改为粗体,但这是行不通的。 (it disallows any to be bold).
(不允许任何粗体显示)。
first thing, as a rule of thumb, add classes not css directly from js. 首先,根据经验,不直接从js添加类而不是CSS。 Secondly you are applying the bold rule to
".appname"
and not to "li.appname_getinfo"
其次,您将粗体规则应用于
".appname"
而不是"li.appname_getinfo"
Need to use an $().each
to add handler to each item. 需要使用
$().each
为每个项目添加处理程序。 My code is: 我的代码是:
$('.appName').each(
function(index, item){
$(item).click(
function(e) {
$('.appName').css('fontWeight', 'normal');
$(this).css('fontWeight', 'bold');
});
});
Tested in Chrome and it seems to work as you describe 在Chrome中进行了测试,它似乎可以按照您的描述工作
While I suspect the problem is, as mentioned above, the mismatch in class-names, one other reason could be because you have a container inside of the "li" element and that container is set to font-weight normal. 如上所述,虽然我怀疑问题是类名不匹配,但另一个原因可能是因为您在“ li”元素内有一个容器,并且该容器设置为font-weight normal。 You are setting the bold on the LI element, but if the element inside of it is normal font, then setting it on the LI won't do anything... eg
您正在LI元素上设置粗体,但是如果其中的元素是普通字体,则在LI上设置它将无济于事...例如
...
<style>
span { font-weight:normal; }
</style>
...
<ul>
<li class="appName"><span>this text will not bold when you bold the LI because it's in a span, and spans are styled with normal font-weight</span>
</li>
<ul>
Chrome's dev tools will help... Chrome的开发工具将帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.