简体   繁体   English

使用jQuery更改单击元素的样式并将该样式从具有相同类的其他元素中删除

[英]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.

相关问题 被点击的元素改变其他元素的样式 - Clicked element changes the style of other elements 如何将样式应用于类的单个元素,同时从同一个类的前一个元素中删除样式? - How to apply style to a single element of a class, while removing style from the previous element of the same class? 使用JQuery更改同一类中所有元素的CSS样式 - Use JQuery to change all element's css style in same class 使用 javascript 更改 class 元素数组中第一个元素的样式 - change the style of only first element in an array of class elements using javascript jQuery从许多具有相同.class的元素中更改了Clicked元素的CSS背景 - jQuery change CSS background of clicked element from many elements with same .class 如何使用.classList.add('class')在具有多个相同class名称的元素与javaScript中的其他元素添加样式 - How to add style on element which have multiple same class name with other elements in javaScript using .classList.add(‘class’) 如何更改具有相同类名的元素的样式 - How to change the style of elements with same class name 更改同一个类的多个元素的样式 - Change the style of multiple elements with the same class 单击元素时切换元素的类(或更改样式),Vuejs - Toggle class(or change style) of element when element in clicked, Vuejs 样式textarea内容与使用JavaScript的任何其他元素具有相同的样式? - Style textarea content with the same style as any other element using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM